src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.m
branchmetal-prototype-branch
changeset 57416 e153174dba06
child 57458 3a7c29ba6b1c
equal deleted inserted replaced
57400:978ffc56771f 57416:e153174dba06
       
     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 
       
    30 #include "sun_java2d_pipe_BufferedOpCodes.h"
       
    31 
       
    32 #include "jlong.h"
       
    33 #include "MTLBlitLoops.h"
       
    34 #include "MTLBufImgOps.h"
       
    35 #include "MTLMaskBlit.h"
       
    36 #include "MTLMaskFill.h"
       
    37 #include "MTLPaints.h"
       
    38 #include "MTLRenderQueue.h"
       
    39 #include "MTLRenderer.h"
       
    40 #include "MTLTextRenderer.h"
       
    41 
       
    42 /**
       
    43  * Used to track whether we are in a series of a simple primitive operations
       
    44  * or texturing operations.  This variable should be controlled only via
       
    45  * the INIT/CHECK/RESET_PREVIOUS_OP() macros.  See the
       
    46  * MTLRenderQueue_CheckPreviousOp() method below for more information.
       
    47  */
       
    48 jint previousOp;
       
    49 
       
    50 /**
       
    51  * References to the "current" context and destination surface.
       
    52  */
       
    53 static MTLContext *mtlc = NULL;
       
    54 static BMTLSDOps *dstOps = NULL;
       
    55 
       
    56 /**
       
    57  * The following methods are implemented in the windowing system (i.e. GLX
       
    58  * and WGL) source files.
       
    59  */
       
    60 extern void MTLGC_DestroyMTLGraphicsConfig(jlong pConfigInfo);
       
    61 extern void MTLSD_SwapBuffers(JNIEnv *env, jlong window);
       
    62 
       
    63 /**
       
    64  * Helper methods to manage modified layers
       
    65  */
       
    66 static MTLLayer ** g_modifiedLayers = NULL;
       
    67 static int g_modifiedLayersCount = 0;
       
    68 static int g_modifiedLayersAllocatedCount = 0;
       
    69 
       
    70 static void markLayerModified(MTLLayer * modifiedLayer) {
       
    71     if (modifiedLayer == NULL)
       
    72         return;
       
    73     if (g_modifiedLayers == NULL) {
       
    74         g_modifiedLayersAllocatedCount = 3;
       
    75         g_modifiedLayers = malloc(g_modifiedLayersAllocatedCount * sizeof(MTLLayer *));
       
    76     }
       
    77     for (int c = 0; c < g_modifiedLayersCount; ++c) {
       
    78         if (g_modifiedLayers[c] == modifiedLayer)
       
    79             return;
       
    80     }
       
    81     ++g_modifiedLayersCount;
       
    82     if (g_modifiedLayersCount > g_modifiedLayersAllocatedCount) {
       
    83         g_modifiedLayersAllocatedCount = g_modifiedLayersCount;
       
    84         g_modifiedLayers = realloc(g_modifiedLayers, g_modifiedLayersAllocatedCount * sizeof(MTLLayer *));
       
    85     }
       
    86     g_modifiedLayers[g_modifiedLayersCount - 1] = modifiedLayer;
       
    87 }
       
    88 
       
    89 static void scheduleBlitAllModifiedLayers() {
       
    90     for (int c = 0; c < g_modifiedLayersCount; ++c) {
       
    91         MTLLayer * layer = g_modifiedLayers[c];
       
    92         MTLContext * ctx = layer.ctx;
       
    93         if (layer == NULL || ctx == NULL)
       
    94             continue;
       
    95         id<MTLCommandBuffer> bufferToCommit = ctx.commandBuffer;
       
    96         [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
       
    97             [layer blitTexture:bufferToCommit];
       
    98         }];
       
    99 
       
   100         [ctx releaseCommandBuffer];
       
   101     }
       
   102     g_modifiedLayersCount = 0;
       
   103 }
       
   104 
       
   105 static void onSurfaceModified(BMTLSDOps *bmtldst) {
       
   106     if (bmtldst != NULL && bmtldst->privOps != NULL && ((MTLSDOps *)bmtldst->privOps)->layer != NULL)
       
   107         markLayerModified(((MTLSDOps *) bmtldst->privOps)->layer);
       
   108 }
       
   109 
       
   110 static const jint g_drawOpcodes[] = {
       
   111         sun_java2d_pipe_BufferedOpCodes_DRAW_LINE,
       
   112         sun_java2d_pipe_BufferedOpCodes_DRAW_RECT,
       
   113         sun_java2d_pipe_BufferedOpCodes_DRAW_POLY,
       
   114         sun_java2d_pipe_BufferedOpCodes_DRAW_PIXEL,
       
   115         sun_java2d_pipe_BufferedOpCodes_DRAW_SCANLINES,
       
   116         sun_java2d_pipe_BufferedOpCodes_DRAW_PARALLELOGRAM,
       
   117         sun_java2d_pipe_BufferedOpCodes_DRAW_AAPARALLELOGRAM,
       
   118 
       
   119         sun_java2d_pipe_BufferedOpCodes_DRAW_GLYPH_LIST,
       
   120 
       
   121         sun_java2d_pipe_BufferedOpCodes_FILL_RECT,
       
   122         sun_java2d_pipe_BufferedOpCodes_FILL_SPANS,
       
   123         sun_java2d_pipe_BufferedOpCodes_FILL_PARALLELOGRAM,
       
   124         sun_java2d_pipe_BufferedOpCodes_FILL_AAPARALLELOGRAM,
       
   125 
       
   126         sun_java2d_pipe_BufferedOpCodes_COPY_AREA,
       
   127         sun_java2d_pipe_BufferedOpCodes_MASK_FILL,
       
   128         sun_java2d_pipe_BufferedOpCodes_MASK_BLIT,
       
   129         sun_java2d_pipe_BufferedOpCodes_SET_SHAPE_CLIP_SPANS
       
   130 };
       
   131 
       
   132 static jboolean isDrawOpcode(jint opcode) {
       
   133     for (int c = 0; c < sizeof(g_drawOpcodes)/sizeof(g_drawOpcodes[0]); ++c) {
       
   134         if (opcode == g_drawOpcodes[c])
       
   135             return JNI_TRUE;
       
   136     }
       
   137     return JNI_FALSE;
       
   138 }
       
   139 
       
   140 JNIEXPORT void JNICALL
       
   141 Java_sun_java2d_metal_MTLRenderQueue_flushBuffer
       
   142     (JNIEnv *env, jobject mtlrq,
       
   143      jlong buf, jint limit)
       
   144 {
       
   145     jboolean sync = JNI_FALSE;
       
   146     unsigned char *b, *end;
       
   147 
       
   148     J2dTraceLn1(J2D_TRACE_INFO,
       
   149                 "MTLRenderQueue_flushBuffer: limit=%d", limit);
       
   150 
       
   151     b = (unsigned char *)jlong_to_ptr(buf);
       
   152     if (b == NULL) {
       
   153         J2dRlsTraceLn(J2D_TRACE_ERROR,
       
   154             "MTLRenderQueue_flushBuffer: cannot get direct buffer address");
       
   155         return;
       
   156     }
       
   157 
       
   158     INIT_PREVIOUS_OP();
       
   159     end = b + limit;
       
   160 
       
   161     while (b < end) {
       
   162         jint opcode = NEXT_INT(b);
       
   163 
       
   164         J2dTraceLn2(J2D_TRACE_VERBOSE,
       
   165                     "MTLRenderQueue_flushBuffer: opcode=%d, rem=%d",
       
   166                     opcode, (end-b));
       
   167 
       
   168         if (opcode != sun_java2d_pipe_BufferedOpCodes_DRAW_GLYPH_LIST &&
       
   169             opcode != sun_java2d_pipe_BufferedOpCodes_NOOP)
       
   170         {
       
   171             //MTLTR_DisableGlyphModeState();
       
   172         }
       
   173 
       
   174         switch (opcode) {
       
   175 
       
   176         // draw ops
       
   177         case sun_java2d_pipe_BufferedOpCodes_DRAW_LINE:
       
   178             {
       
   179                 J2dTraceLn(J2D_TRACE_VERBOSE, "sun_java2d_pipe_BufferedOpCodes_DRAW_LINE");
       
   180                 jint x1 = NEXT_INT(b);
       
   181                 jint y1 = NEXT_INT(b);
       
   182                 jint x2 = NEXT_INT(b);
       
   183                 jint y2 = NEXT_INT(b);
       
   184                 MTLRenderer_DrawLine(mtlc, dstOps, x1, y1, x2, y2);
       
   185             }
       
   186             break;
       
   187         case sun_java2d_pipe_BufferedOpCodes_DRAW_RECT:
       
   188             {
       
   189                 jint x = NEXT_INT(b);
       
   190                 jint y = NEXT_INT(b);
       
   191                 jint w = NEXT_INT(b);
       
   192                 jint h = NEXT_INT(b);
       
   193                 MTLRenderer_DrawRect(mtlc, dstOps, x, y, w, h);
       
   194             }
       
   195             break;
       
   196         case sun_java2d_pipe_BufferedOpCodes_DRAW_POLY:
       
   197             {
       
   198                 jint nPoints      = NEXT_INT(b);
       
   199                 jboolean isClosed = NEXT_BOOLEAN(b);
       
   200                 jint transX       = NEXT_INT(b);
       
   201                 jint transY       = NEXT_INT(b);
       
   202                 jint *xPoints = (jint *)b;
       
   203                 jint *yPoints = ((jint *)b) + nPoints;
       
   204                 MTLRenderer_DrawPoly(mtlc, dstOps, nPoints, isClosed, transX, transY, xPoints, yPoints);
       
   205                 SKIP_BYTES(b, nPoints * BYTES_PER_POLY_POINT);
       
   206             }
       
   207             break;
       
   208         case sun_java2d_pipe_BufferedOpCodes_DRAW_PIXEL:
       
   209             {
       
   210                 jint x = NEXT_INT(b);
       
   211                 jint y = NEXT_INT(b);
       
   212                 CONTINUE_IF_NULL(mtlc);
       
   213                 //TODO
       
   214             }
       
   215             break;
       
   216         case sun_java2d_pipe_BufferedOpCodes_DRAW_SCANLINES:
       
   217             {
       
   218                 jint count = NEXT_INT(b);
       
   219                 MTLRenderer_DrawScanlines(mtlc, dstOps, count, (jint *)b);
       
   220 
       
   221                 SKIP_BYTES(b, count * BYTES_PER_SCANLINE);
       
   222             }
       
   223             break;
       
   224         case sun_java2d_pipe_BufferedOpCodes_DRAW_PARALLELOGRAM:
       
   225             {
       
   226                 jfloat x11 = NEXT_FLOAT(b);
       
   227                 jfloat y11 = NEXT_FLOAT(b);
       
   228                 jfloat dx21 = NEXT_FLOAT(b);
       
   229                 jfloat dy21 = NEXT_FLOAT(b);
       
   230                 jfloat dx12 = NEXT_FLOAT(b);
       
   231                 jfloat dy12 = NEXT_FLOAT(b);
       
   232                 jfloat lwr21 = NEXT_FLOAT(b);
       
   233                 jfloat lwr12 = NEXT_FLOAT(b);
       
   234 
       
   235                 MTLRenderer_DrawParallelogram(mtlc, dstOps,
       
   236                                               x11, y11,
       
   237                                               dx21, dy21,
       
   238                                               dx12, dy12,
       
   239                                               lwr21, lwr12);
       
   240             }
       
   241             break;
       
   242         case sun_java2d_pipe_BufferedOpCodes_DRAW_AAPARALLELOGRAM:
       
   243             {
       
   244                 jfloat x11 = NEXT_FLOAT(b);
       
   245                 jfloat y11 = NEXT_FLOAT(b);
       
   246                 jfloat dx21 = NEXT_FLOAT(b);
       
   247                 jfloat dy21 = NEXT_FLOAT(b);
       
   248                 jfloat dx12 = NEXT_FLOAT(b);
       
   249                 jfloat dy12 = NEXT_FLOAT(b);
       
   250                 jfloat lwr21 = NEXT_FLOAT(b);
       
   251                 jfloat lwr12 = NEXT_FLOAT(b);
       
   252 
       
   253                 MTLRenderer_DrawAAParallelogram(mtlc, dstOps,
       
   254                                                 x11, y11,
       
   255                                                 dx21, dy21,
       
   256                                                 dx12, dy12,
       
   257                                                 lwr21, lwr12);
       
   258             }
       
   259             break;
       
   260 
       
   261         // fill ops
       
   262         case sun_java2d_pipe_BufferedOpCodes_FILL_RECT:
       
   263             {
       
   264                 jint x = NEXT_INT(b);
       
   265                 jint y = NEXT_INT(b);
       
   266                 jint w = NEXT_INT(b);
       
   267                 jint h = NEXT_INT(b);
       
   268                 MTLRenderer_FillRect(mtlc, dstOps, x, y, w, h);
       
   269             }
       
   270             break;
       
   271         case sun_java2d_pipe_BufferedOpCodes_FILL_SPANS:
       
   272             {
       
   273                 jint count = NEXT_INT(b);
       
   274                 MTLRenderer_FillSpans(mtlc, dstOps, count, (jint *)b);
       
   275                 SKIP_BYTES(b, count * BYTES_PER_SPAN);
       
   276             }
       
   277             break;
       
   278         case sun_java2d_pipe_BufferedOpCodes_FILL_PARALLELOGRAM:
       
   279             {
       
   280                 jfloat x11 = NEXT_FLOAT(b);
       
   281                 jfloat y11 = NEXT_FLOAT(b);
       
   282                 jfloat dx21 = NEXT_FLOAT(b);
       
   283                 jfloat dy21 = NEXT_FLOAT(b);
       
   284                 jfloat dx12 = NEXT_FLOAT(b);
       
   285                 jfloat dy12 = NEXT_FLOAT(b);
       
   286                 MTLRenderer_FillParallelogram(mtlc, dstOps,
       
   287                                               x11, y11,
       
   288                                               dx21, dy21,
       
   289                                               dx12, dy12);
       
   290             }
       
   291             break;
       
   292         case sun_java2d_pipe_BufferedOpCodes_FILL_AAPARALLELOGRAM:
       
   293             {
       
   294                 jfloat x11 = NEXT_FLOAT(b);
       
   295                 jfloat y11 = NEXT_FLOAT(b);
       
   296                 jfloat dx21 = NEXT_FLOAT(b);
       
   297                 jfloat dy21 = NEXT_FLOAT(b);
       
   298                 jfloat dx12 = NEXT_FLOAT(b);
       
   299                 jfloat dy12 = NEXT_FLOAT(b);
       
   300                 MTLRenderer_FillAAParallelogram(mtlc, dstOps,
       
   301                                                 x11, y11,
       
   302                                                 dx21, dy21,
       
   303                                                 dx12, dy12);
       
   304             }
       
   305             break;
       
   306 
       
   307         // text-related ops
       
   308         case sun_java2d_pipe_BufferedOpCodes_DRAW_GLYPH_LIST:
       
   309             {
       
   310                 jint numGlyphs        = NEXT_INT(b);
       
   311                 jint packedParams     = NEXT_INT(b);
       
   312                 jfloat glyphListOrigX = NEXT_FLOAT(b);
       
   313                 jfloat glyphListOrigY = NEXT_FLOAT(b);
       
   314                 jboolean usePositions = EXTRACT_BOOLEAN(packedParams,
       
   315                                                         OFFSET_POSITIONS);
       
   316                 jboolean subPixPos    = EXTRACT_BOOLEAN(packedParams,
       
   317                                                         OFFSET_SUBPIXPOS);
       
   318                 jboolean rgbOrder     = EXTRACT_BOOLEAN(packedParams,
       
   319                                                         OFFSET_RGBORDER);
       
   320                 jint lcdContrast      = EXTRACT_BYTE(packedParams,
       
   321                                                      OFFSET_CONTRAST);
       
   322                 unsigned char *images = b;
       
   323                 unsigned char *positions;
       
   324                 jint bytesPerGlyph;
       
   325                 if (usePositions) {
       
   326                     positions = (b + numGlyphs * BYTES_PER_GLYPH_IMAGE);
       
   327                     bytesPerGlyph = BYTES_PER_POSITIONED_GLYPH;
       
   328                 } else {
       
   329                     positions = NULL;
       
   330                     bytesPerGlyph = BYTES_PER_GLYPH_IMAGE;
       
   331                 }
       
   332                 MTLTR_DrawGlyphList(env, mtlc, dstOps,
       
   333                                     numGlyphs, usePositions,
       
   334                                     subPixPos, rgbOrder, lcdContrast,
       
   335                                     glyphListOrigX, glyphListOrigY,
       
   336                                     images, positions);
       
   337                 SKIP_BYTES(b, numGlyphs * bytesPerGlyph);
       
   338             }
       
   339             break;
       
   340 
       
   341         // copy-related ops
       
   342         case sun_java2d_pipe_BufferedOpCodes_COPY_AREA:
       
   343             {
       
   344                 jint x  = NEXT_INT(b);
       
   345                 jint y  = NEXT_INT(b);
       
   346                 jint w  = NEXT_INT(b);
       
   347                 jint h  = NEXT_INT(b);
       
   348                 jint dx = NEXT_INT(b);
       
   349                 jint dy = NEXT_INT(b);
       
   350                 MTLBlitLoops_CopyArea(env, mtlc, dstOps,
       
   351                                       x, y, w, h, dx, dy);
       
   352             }
       
   353             break;
       
   354         case sun_java2d_pipe_BufferedOpCodes_BLIT:
       
   355             {
       
   356 
       
   357                 jint packedParams = NEXT_INT(b);
       
   358                 jint sx1          = NEXT_INT(b);
       
   359                 jint sy1          = NEXT_INT(b);
       
   360                 jint sx2          = NEXT_INT(b);
       
   361                 jint sy2          = NEXT_INT(b);
       
   362                 jdouble dx1       = NEXT_DOUBLE(b);
       
   363                 jdouble dy1       = NEXT_DOUBLE(b);
       
   364                 jdouble dx2       = NEXT_DOUBLE(b);
       
   365                 jdouble dy2       = NEXT_DOUBLE(b);
       
   366                 jlong pSrc        = NEXT_LONG(b);
       
   367                 jlong pDst        = NEXT_LONG(b);
       
   368                 jint hint         = EXTRACT_BYTE(packedParams, OFFSET_HINT);
       
   369                 jboolean texture  = EXTRACT_BOOLEAN(packedParams,
       
   370                                                     OFFSET_TEXTURE);
       
   371                 jboolean rtt      = EXTRACT_BOOLEAN(packedParams,
       
   372                                                     OFFSET_RTT);
       
   373                 jboolean xform    = EXTRACT_BOOLEAN(packedParams,
       
   374                                                     OFFSET_XFORM);
       
   375                 jboolean isoblit  = EXTRACT_BOOLEAN(packedParams,
       
   376                                                     OFFSET_ISOBLIT);
       
   377                 if (isoblit) {
       
   378                     MTLBlitLoops_IsoBlit(env, mtlc, pSrc, pDst,
       
   379                                          xform, hint, texture, rtt,
       
   380                                          sx1, sy1, sx2, sy2,
       
   381                                          dx1, dy1, dx2, dy2);
       
   382                 } else {
       
   383                     jint srctype = EXTRACT_BYTE(packedParams, OFFSET_SRCTYPE);
       
   384                     MTLBlitLoops_Blit(env, mtlc, pSrc, pDst,
       
   385                                       xform, hint, srctype, texture,
       
   386                                       sx1, sy1, sx2, sy2,
       
   387                                       dx1, dy1, dx2, dy2);
       
   388                 }
       
   389                 onSurfaceModified(jlong_to_ptr(pDst));
       
   390             }
       
   391             break;
       
   392         case sun_java2d_pipe_BufferedOpCodes_SURFACE_TO_SW_BLIT:
       
   393             {
       
   394 
       
   395                 jint sx      = NEXT_INT(b);
       
   396                 jint sy      = NEXT_INT(b);
       
   397                 jint dx      = NEXT_INT(b);
       
   398                 jint dy      = NEXT_INT(b);
       
   399                 jint w       = NEXT_INT(b);
       
   400                 jint h       = NEXT_INT(b);
       
   401                 jint dsttype = NEXT_INT(b);
       
   402                 jlong pSrc   = NEXT_LONG(b);
       
   403                 jlong pDst   = NEXT_LONG(b);
       
   404                 MTLBlitLoops_SurfaceToSwBlit(env, mtlc,
       
   405                                              pSrc, pDst, dsttype,
       
   406                                              sx, sy, dx, dy, w, h);
       
   407             }
       
   408             break;
       
   409         case sun_java2d_pipe_BufferedOpCodes_MASK_FILL:
       
   410             {
       
   411 
       
   412                 jint x        = NEXT_INT(b);
       
   413                 jint y        = NEXT_INT(b);
       
   414                 jint w        = NEXT_INT(b);
       
   415                 jint h        = NEXT_INT(b);
       
   416                 jint maskoff  = NEXT_INT(b);
       
   417                 jint maskscan = NEXT_INT(b);
       
   418                 jint masklen  = NEXT_INT(b);
       
   419                 unsigned char *pMask = (masklen > 0) ? b : NULL;
       
   420                 MTLMaskFill_MaskFill(mtlc, dstOps, x, y, w, h,
       
   421                                      maskoff, maskscan, masklen, pMask);
       
   422                 SKIP_BYTES(b, masklen);
       
   423             }
       
   424             break;
       
   425         case sun_java2d_pipe_BufferedOpCodes_MASK_BLIT:
       
   426             {
       
   427 
       
   428                 jint dstx     = NEXT_INT(b);
       
   429                 jint dsty     = NEXT_INT(b);
       
   430                 jint width    = NEXT_INT(b);
       
   431                 jint height   = NEXT_INT(b);
       
   432                 jint masklen  = width * height * sizeof(jint);
       
   433                 MTLMaskBlit_MaskBlit(env, mtlc, dstOps,
       
   434                                      dstx, dsty, width, height, b);
       
   435                 SKIP_BYTES(b, masklen);
       
   436             }
       
   437             break;
       
   438 
       
   439         // state-related ops
       
   440         case sun_java2d_pipe_BufferedOpCodes_SET_RECT_CLIP:
       
   441             {
       
   442                 jint x1 = NEXT_INT(b);
       
   443                 jint y1 = NEXT_INT(b);
       
   444                 jint x2 = NEXT_INT(b);
       
   445                 jint y2 = NEXT_INT(b);
       
   446                 [mtlc setClipRectX1:x1 Y1:y1 X2:x2 Y2:y2];
       
   447             }
       
   448             break;
       
   449         case sun_java2d_pipe_BufferedOpCodes_BEGIN_SHAPE_CLIP:
       
   450             {
       
   451                 [mtlc beginShapeClip];
       
   452             }
       
   453             break;
       
   454         case sun_java2d_pipe_BufferedOpCodes_SET_SHAPE_CLIP_SPANS:
       
   455             {
       
   456                 jint count = NEXT_INT(b);
       
   457                 MTLRenderer_FillSpans(mtlc, dstOps, count, (jint *)b);
       
   458                 SKIP_BYTES(b, count * BYTES_PER_SPAN);
       
   459             }
       
   460             break;
       
   461         case sun_java2d_pipe_BufferedOpCodes_END_SHAPE_CLIP:
       
   462             {
       
   463                 //TODO
       
   464                 [mtlc endShapeClipDstOps:dstOps];
       
   465             }
       
   466             break;
       
   467         case sun_java2d_pipe_BufferedOpCodes_RESET_CLIP:
       
   468             {
       
   469                 [mtlc resetClip];
       
   470             }
       
   471             break;
       
   472         case sun_java2d_pipe_BufferedOpCodes_SET_ALPHA_COMPOSITE:
       
   473             {
       
   474                 jint rule         = NEXT_INT(b);
       
   475                 jfloat extraAlpha = NEXT_FLOAT(b);
       
   476                 jint flags        = NEXT_INT(b);
       
   477                 [mtlc setAlphaCompositeRule:rule extraAlpha:extraAlpha flags:flags];
       
   478             }
       
   479             break;
       
   480         case sun_java2d_pipe_BufferedOpCodes_SET_XOR_COMPOSITE:
       
   481             {
       
   482                 jint xorPixel = NEXT_INT(b);
       
   483                 [mtlc setXorComposite:xorPixel];
       
   484             }
       
   485             break;
       
   486         case sun_java2d_pipe_BufferedOpCodes_RESET_COMPOSITE:
       
   487             {
       
   488                 [mtlc resetComposite];
       
   489             }
       
   490             break;
       
   491         case sun_java2d_pipe_BufferedOpCodes_SET_TRANSFORM:
       
   492             {
       
   493                 jdouble m00 = NEXT_DOUBLE(b);
       
   494                 jdouble m10 = NEXT_DOUBLE(b);
       
   495                 jdouble m01 = NEXT_DOUBLE(b);
       
   496                 jdouble m11 = NEXT_DOUBLE(b);
       
   497                 jdouble m02 = NEXT_DOUBLE(b);
       
   498                 jdouble m12 = NEXT_DOUBLE(b);
       
   499                 [mtlc setTransformM00:m00 M10:m10 M01:m01 M11:m11 M02:m02 M12:m12];
       
   500             }
       
   501             break;
       
   502         case sun_java2d_pipe_BufferedOpCodes_RESET_TRANSFORM:
       
   503             {
       
   504                 [mtlc resetTransform];
       
   505             }
       
   506             break;
       
   507 
       
   508         // context-related ops
       
   509         case sun_java2d_pipe_BufferedOpCodes_SET_SURFACES:
       
   510             {
       
   511 
       
   512                 jlong pSrc = NEXT_LONG(b);
       
   513                 jlong pDst = NEXT_LONG(b);
       
   514                 if (mtlc != NULL) {
       
   515                     RESET_PREVIOUS_OP();
       
   516                 }
       
   517 
       
   518                 dstOps = (BMTLSDOps *)jlong_to_ptr(pDst);
       
   519                 [MTLContext setSurfacesEnv:env src:pSrc dst:pDst];
       
   520             }
       
   521             break;
       
   522         case sun_java2d_pipe_BufferedOpCodes_SET_SCRATCH_SURFACE:
       
   523             {
       
   524                 jlong pConfigInfo = NEXT_LONG(b);
       
   525                 MTLGraphicsConfigInfo *mtlInfo =
       
   526                         (MTLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
       
   527 
       
   528                 if (mtlInfo == NULL) {
       
   529 
       
   530                 } else {
       
   531                     MTLContext *newMtlc = mtlInfo->context;
       
   532                     if (newMtlc == NULL) {
       
   533 
       
   534                     } else {
       
   535                         mtlc = newMtlc;
       
   536                         dstOps = NULL;
       
   537                     }
       
   538                 }
       
   539             }
       
   540             break;
       
   541         case sun_java2d_pipe_BufferedOpCodes_FLUSH_SURFACE:
       
   542             {
       
   543                 jlong pData = NEXT_LONG(b);
       
   544                 BMTLSDOps *mtlsdo = (BMTLSDOps *)jlong_to_ptr(pData);
       
   545                 if (mtlsdo != NULL) {
       
   546                     CONTINUE_IF_NULL(mtlc);
       
   547                     RESET_PREVIOUS_OP();
       
   548                     MTLSD_Delete(env, mtlsdo);
       
   549                 }
       
   550             }
       
   551             break;
       
   552         case sun_java2d_pipe_BufferedOpCodes_DISPOSE_SURFACE:
       
   553             {
       
   554                 jlong pData = NEXT_LONG(b);
       
   555                 BMTLSDOps *mtlsdo = (BMTLSDOps *)jlong_to_ptr(pData);
       
   556                 if (mtlsdo != NULL) {
       
   557                     CONTINUE_IF_NULL(mtlc);
       
   558                     RESET_PREVIOUS_OP();
       
   559                     MTLSD_Delete(env, mtlsdo);
       
   560                     if (mtlsdo->privOps != NULL) {
       
   561                         free(mtlsdo->privOps);
       
   562                     }
       
   563                 }
       
   564             }
       
   565             break;
       
   566         case sun_java2d_pipe_BufferedOpCodes_DISPOSE_CONFIG:
       
   567             {
       
   568                 jlong pConfigInfo = NEXT_LONG(b);
       
   569                 CONTINUE_IF_NULL(mtlc);
       
   570                 RESET_PREVIOUS_OP();
       
   571                 [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
       
   572                     MTLGC_DestroyMTLGraphicsConfig(pConfigInfo);
       
   573                 }];
       
   574 
       
   575 
       
   576                 // the previous method will call glX/wglMakeCurrent(None),
       
   577                 // so we should nullify the current mtlc and dstOps to avoid
       
   578                 // calling glFlush() (or similar) while no context is current
       
   579                 mtlc = NULL;
       
   580              //   dstOps = NULL;
       
   581             }
       
   582             break;
       
   583         case sun_java2d_pipe_BufferedOpCodes_INVALIDATE_CONTEXT:
       
   584             {
       
   585                 //TODO
       
   586                 // flush just in case there are any pending operations in
       
   587                 // the hardware pipe
       
   588                 if (mtlc != NULL) {
       
   589                     RESET_PREVIOUS_OP();
       
   590                 }
       
   591                 // invalidate the references to the current context and
       
   592                 // destination surface that are maintained at the native level
       
   593                 mtlc = NULL;
       
   594             //    dstOps = NULL;
       
   595             }
       
   596             break;
       
   597         case sun_java2d_pipe_BufferedOpCodes_SAVE_STATE:
       
   598             {
       
   599                 //TODO
       
   600             }
       
   601             break;
       
   602 
       
   603         case sun_java2d_pipe_BufferedOpCodes_RESTORE_STATE:
       
   604             {
       
   605                 //TODO
       
   606 
       
   607             }
       
   608             break;
       
   609         case sun_java2d_pipe_BufferedOpCodes_SYNC:
       
   610             {
       
   611                 sync = JNI_TRUE;
       
   612             }
       
   613             break;
       
   614 
       
   615         // multibuffering ops
       
   616         case sun_java2d_pipe_BufferedOpCodes_SWAP_BUFFERS:
       
   617             {
       
   618                 jlong window = NEXT_LONG(b);
       
   619                 if (mtlc != NULL) {
       
   620                     RESET_PREVIOUS_OP();
       
   621                 }
       
   622                 MTLSD_SwapBuffers(env, window);
       
   623             }
       
   624             break;
       
   625 
       
   626         // special no-op (mainly used for achieving 8-byte alignment)
       
   627         case sun_java2d_pipe_BufferedOpCodes_NOOP:
       
   628             break;
       
   629 
       
   630         // paint-related ops
       
   631         case sun_java2d_pipe_BufferedOpCodes_RESET_PAINT:
       
   632             {
       
   633                 MTLPaints_ResetPaint(mtlc);
       
   634             }
       
   635             break;
       
   636         case sun_java2d_pipe_BufferedOpCodes_SET_COLOR:
       
   637             {
       
   638                 jint pixel = NEXT_INT(b);
       
   639 
       
   640                 if (dstOps != NULL) {
       
   641                     MTLSDOps *dstCGLOps = (MTLSDOps *)dstOps->privOps;
       
   642                     dstCGLOps->configInfo->context.color = pixel;
       
   643                 }
       
   644                 MTLPaints_SetColor(mtlc, pixel);
       
   645             }
       
   646             break;
       
   647         case sun_java2d_pipe_BufferedOpCodes_SET_GRADIENT_PAINT:
       
   648             {
       
   649                 jboolean useMask= NEXT_BOOLEAN(b);
       
   650                 jboolean cyclic = NEXT_BOOLEAN(b);
       
   651                 jdouble p0      = NEXT_DOUBLE(b);
       
   652                 jdouble p1      = NEXT_DOUBLE(b);
       
   653                 jdouble p3      = NEXT_DOUBLE(b);
       
   654                 jint pixel1     = NEXT_INT(b);
       
   655                 jint pixel2     = NEXT_INT(b);
       
   656                 if (dstOps != NULL) {
       
   657                     MTLSDOps *dstCGLOps = (MTLSDOps *)dstOps->privOps;
       
   658                     [dstCGLOps->configInfo->context setGradientPaintUseMask:useMask cyclic:cyclic
       
   659                                                                          p0:p0 p1:p1 p3:p3
       
   660                                                                      pixel1:pixel1 pixel2:pixel2];
       
   661 
       
   662                 }
       
   663             }
       
   664             break;
       
   665         case sun_java2d_pipe_BufferedOpCodes_SET_LINEAR_GRADIENT_PAINT:
       
   666             {
       
   667                 jboolean useMask = NEXT_BOOLEAN(b);
       
   668                 jboolean linear  = NEXT_BOOLEAN(b);
       
   669                 jint cycleMethod = NEXT_INT(b);
       
   670                 jint numStops    = NEXT_INT(b);
       
   671                 jfloat p0        = NEXT_FLOAT(b);
       
   672                 jfloat p1        = NEXT_FLOAT(b);
       
   673                 jfloat p3        = NEXT_FLOAT(b);
       
   674                 void *fractions, *pixels;
       
   675                 fractions = b; SKIP_BYTES(b, numStops * sizeof(jfloat));
       
   676                 pixels    = b; SKIP_BYTES(b, numStops * sizeof(jint));
       
   677                 MTLPaints_SetLinearGradientPaint(mtlc, dstOps,
       
   678                                                  useMask, linear,
       
   679                                                  cycleMethod, numStops,
       
   680                                                  p0, p1, p3,
       
   681                                                  fractions, pixels);
       
   682             }
       
   683             break;
       
   684         case sun_java2d_pipe_BufferedOpCodes_SET_RADIAL_GRADIENT_PAINT:
       
   685             {
       
   686                 jboolean useMask = NEXT_BOOLEAN(b);
       
   687                 jboolean linear  = NEXT_BOOLEAN(b);
       
   688                 jint numStops    = NEXT_INT(b);
       
   689                 jint cycleMethod = NEXT_INT(b);
       
   690                 jfloat m00       = NEXT_FLOAT(b);
       
   691                 jfloat m01       = NEXT_FLOAT(b);
       
   692                 jfloat m02       = NEXT_FLOAT(b);
       
   693                 jfloat m10       = NEXT_FLOAT(b);
       
   694                 jfloat m11       = NEXT_FLOAT(b);
       
   695                 jfloat m12       = NEXT_FLOAT(b);
       
   696                 jfloat focusX    = NEXT_FLOAT(b);
       
   697                 void *fractions, *pixels;
       
   698                 fractions = b; SKIP_BYTES(b, numStops * sizeof(jfloat));
       
   699                 pixels    = b; SKIP_BYTES(b, numStops * sizeof(jint));
       
   700                 MTLPaints_SetRadialGradientPaint(mtlc, dstOps,
       
   701                                                  useMask, linear,
       
   702                                                  cycleMethod, numStops,
       
   703                                                  m00, m01, m02,
       
   704                                                  m10, m11, m12,
       
   705                                                  focusX,
       
   706                                                  fractions, pixels);
       
   707             }
       
   708             break;
       
   709         case sun_java2d_pipe_BufferedOpCodes_SET_TEXTURE_PAINT:
       
   710             {
       
   711                 jboolean useMask= NEXT_BOOLEAN(b);
       
   712                 jboolean filter = NEXT_BOOLEAN(b);
       
   713                 jlong pSrc      = NEXT_LONG(b);
       
   714                 jdouble xp0     = NEXT_DOUBLE(b);
       
   715                 jdouble xp1     = NEXT_DOUBLE(b);
       
   716                 jdouble xp3     = NEXT_DOUBLE(b);
       
   717                 jdouble yp0     = NEXT_DOUBLE(b);
       
   718                 jdouble yp1     = NEXT_DOUBLE(b);
       
   719                 jdouble yp3     = NEXT_DOUBLE(b);
       
   720                 MTLPaints_SetTexturePaint(mtlc, useMask, pSrc, filter,
       
   721                                           xp0, xp1, xp3,
       
   722                                           yp0, yp1, yp3);
       
   723             }
       
   724             break;
       
   725 
       
   726         // BufferedImageOp-related ops
       
   727         case sun_java2d_pipe_BufferedOpCodes_ENABLE_CONVOLVE_OP:
       
   728             {
       
   729                 jlong pSrc        = NEXT_LONG(b);
       
   730                 jboolean edgeZero = NEXT_BOOLEAN(b);
       
   731                 jint kernelWidth  = NEXT_INT(b);
       
   732                 jint kernelHeight = NEXT_INT(b);
       
   733                 MTLBufImgOps_EnableConvolveOp(mtlc, pSrc, edgeZero,
       
   734                                               kernelWidth, kernelHeight, b);
       
   735                 SKIP_BYTES(b, kernelWidth * kernelHeight * sizeof(jfloat));
       
   736             }
       
   737             break;
       
   738         case sun_java2d_pipe_BufferedOpCodes_DISABLE_CONVOLVE_OP:
       
   739             {
       
   740                 MTLBufImgOps_DisableConvolveOp(mtlc);
       
   741             }
       
   742             break;
       
   743         case sun_java2d_pipe_BufferedOpCodes_ENABLE_RESCALE_OP:
       
   744             {
       
   745                 jlong pSrc          = NEXT_LONG(b);
       
   746                 jboolean nonPremult = NEXT_BOOLEAN(b);
       
   747                 jint numFactors     = 4;
       
   748                 unsigned char *scaleFactors = b;
       
   749                 unsigned char *offsets = (b + numFactors * sizeof(jfloat));
       
   750                 MTLBufImgOps_EnableRescaleOp(mtlc, pSrc, nonPremult,
       
   751                                              scaleFactors, offsets);
       
   752                 SKIP_BYTES(b, numFactors * sizeof(jfloat) * 2);
       
   753             }
       
   754             break;
       
   755         case sun_java2d_pipe_BufferedOpCodes_DISABLE_RESCALE_OP:
       
   756             {
       
   757                 MTLBufImgOps_DisableRescaleOp(mtlc);
       
   758             }
       
   759             break;
       
   760         case sun_java2d_pipe_BufferedOpCodes_ENABLE_LOOKUP_OP:
       
   761             {
       
   762                 jlong pSrc          = NEXT_LONG(b);
       
   763                 jboolean nonPremult = NEXT_BOOLEAN(b);
       
   764                 jboolean shortData  = NEXT_BOOLEAN(b);
       
   765                 jint numBands       = NEXT_INT(b);
       
   766                 jint bandLength     = NEXT_INT(b);
       
   767                 jint offset         = NEXT_INT(b);
       
   768                 jint bytesPerElem = shortData ? sizeof(jshort):sizeof(jbyte);
       
   769                 void *tableValues = b;
       
   770                 MTLBufImgOps_EnableLookupOp(mtlc, pSrc, nonPremult, shortData,
       
   771                                             numBands, bandLength, offset,
       
   772                                             tableValues);
       
   773                 SKIP_BYTES(b, numBands * bandLength * bytesPerElem);
       
   774             }
       
   775             break;
       
   776         case sun_java2d_pipe_BufferedOpCodes_DISABLE_LOOKUP_OP:
       
   777             {
       
   778                 MTLBufImgOps_DisableLookupOp(mtlc);
       
   779             }
       
   780             break;
       
   781 
       
   782         default:
       
   783             J2dRlsTraceLn1(J2D_TRACE_ERROR,
       
   784                 "MTLRenderQueue_flushBuffer: invalid opcode=%d", opcode);
       
   785             if (mtlc != NULL) {
       
   786                 RESET_PREVIOUS_OP();
       
   787             }
       
   788             return;
       
   789         }
       
   790 
       
   791         if (isDrawOpcode(opcode)) // performed rendering operation on dstOps
       
   792             onSurfaceModified(dstOps);
       
   793     }
       
   794 
       
   795     MTLTR_DisableGlyphModeState();
       
   796     scheduleBlitAllModifiedLayers();
       
   797 }
       
   798 
       
   799 /**
       
   800  * Returns a pointer to the "current" context, as set by the last SET_SURFACES
       
   801  * or SET_SCRATCH_SURFACE operation.
       
   802  */
       
   803 MTLContext *
       
   804 MTLRenderQueue_GetCurrentContext()
       
   805 {
       
   806     return mtlc;
       
   807 }
       
   808 
       
   809 /**
       
   810  * Returns a pointer to the "current" destination surface, as set by the last
       
   811  * SET_SURFACES operation.
       
   812  */
       
   813 BMTLSDOps *
       
   814 MTLRenderQueue_GetCurrentDestination()
       
   815 {
       
   816     return dstOps;
       
   817 }
       
   818 
       
   819 /**
       
   820  * Used to track whether we are within a series of simple primitive operations
       
   821  * or texturing operations.  The op parameter determines the nature of the
       
   822  * operation that is to follow.  Valid values for this op parameter are:
       
   823  */
       
   824 void
       
   825 MTLRenderQueue_CheckPreviousOp(jint op)
       
   826 {
       
   827     //TODO
       
   828     previousOp = op;
       
   829 }
       
   830 
       
   831 #endif /* !HEADLESS */