jdk/src/java.desktop/share/classes/sun/java2d/marlin/MarlinTileGenerator.java
changeset 39519 21bfc4452441
parent 34417 57a3863abbb4
child 47126 188ef162f019
equal deleted inserted replaced
39518:cf5567d544b4 39519:21bfc4452441
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    35 
    35 
    36     private final Renderer rdr;
    36     private final Renderer rdr;
    37     private final MarlinCache cache;
    37     private final MarlinCache cache;
    38     private int x, y;
    38     private int x, y;
    39 
    39 
       
    40     // per-thread renderer context
       
    41     final RendererContext rdrCtx;
       
    42 
    40     MarlinTileGenerator(Renderer r) {
    43     MarlinTileGenerator(Renderer r) {
    41         this.rdr = r;
    44         this.rdr = r;
    42         this.cache = r.cache;
    45         this.cache = r.cache;
       
    46         this.rdrCtx = r.rdrCtx;
    43     }
    47     }
    44 
    48 
    45     MarlinTileGenerator init() {
    49     MarlinTileGenerator init() {
    46         this.x = cache.bboxX0;
    50         this.x = cache.bboxX0;
    47         this.y = cache.bboxY0;
    51         this.y = cache.bboxY0;
    53      * Disposes this tile generator:
    57      * Disposes this tile generator:
    54      * clean up before reusing this instance
    58      * clean up before reusing this instance
    55      */
    59      */
    56     @Override
    60     @Override
    57     public void dispose() {
    61     public void dispose() {
    58         if (doMonitors) {
    62         if (DO_MONITORS) {
    59             // called from AAShapePipe.renderTiles() (render tiles end):
    63             // called from AAShapePipe.renderTiles() (render tiles end):
    60             RendererContext.stats.mon_pipe_renderTiles.stop();
    64             rdrCtx.stats.mon_pipe_renderTiles.stop();
    61         }
    65         }
    62         // dispose cache:
    66         // dispose cache:
    63         cache.dispose();
    67         cache.dispose();
    64         // dispose renderer:
    68         // dispose renderer:
    65         rdr.dispose();
    69         rdr.dispose();
    66         // recycle the RendererContext instance
    70         // recycle the RendererContext instance
    67         MarlinRenderingEngine.returnRendererContext(rdr.rdrCtx);
    71         MarlinRenderingEngine.returnRendererContext(rdrCtx);
    68     }
    72     }
    69 
    73 
    70     void getBbox(int bbox[]) {
    74     void getBbox(int[] bbox) {
    71         bbox[0] = cache.bboxX0;
    75         bbox[0] = cache.bboxX0;
    72         bbox[1] = cache.bboxY0;
    76         bbox[1] = cache.bboxY0;
    73         bbox[2] = cache.bboxX1;
    77         bbox[2] = cache.bboxX1;
    74         bbox[3] = cache.bboxY1;
    78         bbox[3] = cache.bboxY1;
    75     }
    79     }
    78      * Gets the width of the tiles that the generator batches output into.
    82      * Gets the width of the tiles that the generator batches output into.
    79      * @return the width of the standard alpha tile
    83      * @return the width of the standard alpha tile
    80      */
    84      */
    81     @Override
    85     @Override
    82     public int getTileWidth() {
    86     public int getTileWidth() {
    83         if (doMonitors) {
    87         if (DO_MONITORS) {
    84             // called from AAShapePipe.renderTiles() (render tiles start):
    88             // called from AAShapePipe.renderTiles() (render tiles start):
    85             RendererContext.stats.mon_pipe_renderTiles.start();
    89             rdrCtx.stats.mon_pipe_renderTiles.start();
    86         }
    90         }
    87         return TILE_SIZE;
    91         return TILE_SIZE;
    88     }
    92     }
    89 
    93 
    90     /**
    94     /**
   124         // of the current tile. This would eliminate the 2 Math.min calls that
   128         // of the current tile. This would eliminate the 2 Math.min calls that
   125         // would be needed here, since our caller needs to compute these 2
   129         // would be needed here, since our caller needs to compute these 2
   126         // values anyway.
   130         // values anyway.
   127         final int alpha = (al == 0x00 ? 0x00
   131         final int alpha = (al == 0x00 ? 0x00
   128                               : (al == MAX_TILE_ALPHA_SUM ? 0xff : 0x80));
   132                               : (al == MAX_TILE_ALPHA_SUM ? 0xff : 0x80));
   129         if (doStats) {
   133         if (DO_STATS) {
   130             RendererContext.stats.hist_tile_generator_alpha.add(alpha);
   134             rdrCtx.stats.hist_tile_generator_alpha.add(alpha);
   131         }
   135         }
   132         return alpha;
   136         return alpha;
   133     }
   137     }
   134 
   138 
   135     /**
   139     /**
   155      * Gets the alpha coverage values for the current tile.
   159      * Gets the alpha coverage values for the current tile.
   156      * Either this method, or the nextTile() method should be called
   160      * Either this method, or the nextTile() method should be called
   157      * once per tile, but not both.
   161      * once per tile, but not both.
   158      */
   162      */
   159     @Override
   163     @Override
   160     public void getAlpha(final byte tile[], final int offset,
   164     public void getAlpha(final byte[] tile, final int offset,
   161                                             final int rowstride)
   165                                             final int rowstride)
   162     {
   166     {
   163         if (cache.useRLE) {
   167         if (cache.useRLE) {
   164             getAlphaRLE(tile, offset, rowstride);
   168             getAlphaRLE(tile, offset, rowstride);
   165         } else {
   169         } else {
   170     /**
   174     /**
   171      * Gets the alpha coverage values for the current tile.
   175      * Gets the alpha coverage values for the current tile.
   172      * Either this method, or the nextTile() method should be called
   176      * Either this method, or the nextTile() method should be called
   173      * once per tile, but not both.
   177      * once per tile, but not both.
   174      */
   178      */
   175     private void getAlphaNoRLE(final byte tile[], final int offset,
   179     private void getAlphaNoRLE(final byte[] tile, final int offset,
   176                                final int rowstride)
   180                                final int rowstride)
   177     {
   181     {
   178         if (doMonitors) {
   182         if (DO_MONITORS) {
   179             RendererContext.stats.mon_ptg_getAlpha.start();
   183             rdrCtx.stats.mon_ptg_getAlpha.start();
   180         }
   184         }
   181 
   185 
   182         // local vars for performance:
   186         // local vars for performance:
   183         final MarlinCache _cache = this.cache;
   187         final MarlinCache _cache = this.cache;
   184         final long[] rowAAChunkIndex = _cache.rowAAChunkIndex;
   188         final long[] rowAAChunkIndex = _cache.rowAAChunkIndex;
   190 
   194 
   191         // note: process tile line [0 - 32[
   195         // note: process tile line [0 - 32[
   192         final int y0 = 0;
   196         final int y0 = 0;
   193         final int y1 = FloatMath.min(this.y + TILE_SIZE, _cache.bboxY1) - this.y;
   197         final int y1 = FloatMath.min(this.y + TILE_SIZE, _cache.bboxY1) - this.y;
   194 
   198 
   195         if (doLogBounds) {
   199         if (DO_LOG_BOUNDS) {
   196             MarlinUtils.logInfo("getAlpha = [" + x0 + " ... " + x1
   200             MarlinUtils.logInfo("getAlpha = [" + x0 + " ... " + x1
   197                                 + "[ [" + y0 + " ... " + y1 + "[");
   201                                 + "[ [" + y0 + " ... " + y1 + "[");
   198         }
   202         }
   199 
   203 
   200         final Unsafe _unsafe = OffHeapArray.unsafe;
   204         final Unsafe _unsafe = OffHeapArray.UNSAFE;
   201         final long SIZE = 1L;
   205         final long SIZE = 1L;
   202         final long addr_rowAA = _cache.rowAAChunk.address;
   206         final long addr_rowAA = _cache.rowAAChunk.address;
   203         long addr;
   207         long addr;
   204 
   208 
   205         final int skipRowPixels = (rowstride - (x1 - x0));
   209         final int skipRowPixels = (rowstride - (x1 - x0));
   250             while (cx < x1) {
   254             while (cx < x1) {
   251                 tile[idx++] = 0;
   255                 tile[idx++] = 0;
   252                 cx++;
   256                 cx++;
   253             }
   257             }
   254 
   258 
   255             if (doTrace) {
   259             if (DO_TRACE) {
   256                 for (int i = idx - (x1 - x0); i < idx; i++) {
   260                 for (int i = idx - (x1 - x0); i < idx; i++) {
   257                     System.out.print(hex(tile[i], 2));
   261                     System.out.print(hex(tile[i], 2));
   258                 }
   262                 }
   259                 System.out.println();
   263                 System.out.println();
   260             }
   264             }
   262             idx += skipRowPixels;
   266             idx += skipRowPixels;
   263         }
   267         }
   264 
   268 
   265         nextTile();
   269         nextTile();
   266 
   270 
   267         if (doMonitors) {
   271         if (DO_MONITORS) {
   268             RendererContext.stats.mon_ptg_getAlpha.stop();
   272             rdrCtx.stats.mon_ptg_getAlpha.stop();
   269         }
   273         }
   270     }
   274     }
   271 
   275 
   272     /**
   276     /**
   273      * Gets the alpha coverage values for the current tile.
   277      * Gets the alpha coverage values for the current tile.
   274      * Either this method, or the nextTile() method should be called
   278      * Either this method, or the nextTile() method should be called
   275      * once per tile, but not both.
   279      * once per tile, but not both.
   276      */
   280      */
   277     private void getAlphaRLE(final byte tile[], final int offset,
   281     private void getAlphaRLE(final byte[] tile, final int offset,
   278                              final int rowstride)
   282                              final int rowstride)
   279     {
   283     {
   280         if (doMonitors) {
   284         if (DO_MONITORS) {
   281             RendererContext.stats.mon_ptg_getAlpha.start();
   285             rdrCtx.stats.mon_ptg_getAlpha.start();
   282         }
   286         }
   283 
   287 
   284         // Decode run-length encoded alpha mask data
   288         // Decode run-length encoded alpha mask data
   285         // The data for row j begins at cache.rowOffsetsRLE[j]
   289         // The data for row j begins at cache.rowOffsetsRLE[j]
   286         // and is encoded as a set of 2-byte pairs (val, runLen)
   290         // and is encoded as a set of 2-byte pairs (val, runLen)
   300 
   304 
   301         // note: process tile line [0 - 32[
   305         // note: process tile line [0 - 32[
   302         final int y0 = 0;
   306         final int y0 = 0;
   303         final int y1 = FloatMath.min(this.y + TILE_SIZE, _cache.bboxY1) - this.y;
   307         final int y1 = FloatMath.min(this.y + TILE_SIZE, _cache.bboxY1) - this.y;
   304 
   308 
   305         if (doLogBounds) {
   309         if (DO_LOG_BOUNDS) {
   306             MarlinUtils.logInfo("getAlpha = [" + x0 + " ... " + x1
   310             MarlinUtils.logInfo("getAlpha = [" + x0 + " ... " + x1
   307                                 + "[ [" + y0 + " ... " + y1 + "[");
   311                                 + "[ [" + y0 + " ... " + y1 + "[");
   308         }
   312         }
   309 
   313 
   310         final Unsafe _unsafe = OffHeapArray.unsafe;
   314         final Unsafe _unsafe = OffHeapArray.UNSAFE;
   311         final long SIZE_BYTE = 1L;
   315         final long SIZE_BYTE = 1L;
   312         final long SIZE_INT = 4L;
   316         final long SIZE_INT = 4L;
   313         final long addr_rowAA = _cache.rowAAChunk.address;
   317         final long addr_rowAA = _cache.rowAAChunk.address;
   314         long addr, addr_row, last_addr, addr_end;
   318         long addr, addr_row, last_addr, addr_end;
   315 
   319 
   436             while (cx < x1) {
   440             while (cx < x1) {
   437                 tile[idx++] = 0;
   441                 tile[idx++] = 0;
   438                 cx++;
   442                 cx++;
   439             }
   443             }
   440 
   444 
   441             if (doTrace) {
   445             if (DO_TRACE) {
   442                 for (int i = idx - (x1 - x0); i < idx; i++) {
   446                 for (int i = idx - (x1 - x0); i < idx; i++) {
   443                     System.out.print(hex(tile[i], 2));
   447                     System.out.print(hex(tile[i], 2));
   444                 }
   448                 }
   445                 System.out.println();
   449                 System.out.println();
   446             }
   450             }
   448             idx += skipRowPixels;
   452             idx += skipRowPixels;
   449         }
   453         }
   450 
   454 
   451         nextTile();
   455         nextTile();
   452 
   456 
   453         if (doMonitors) {
   457         if (DO_MONITORS) {
   454             RendererContext.stats.mon_ptg_getAlpha.stop();
   458             rdrCtx.stats.mon_ptg_getAlpha.stop();
   455         }
   459         }
   456     }
   460     }
   457 
   461 
   458     static String hex(int v, int d) {
   462     static String hex(int v, int d) {
   459         String s = Integer.toHexString(v);
   463         String s = Integer.toHexString(v);