Skip to content

Commit

Permalink
JBR-5638: improved renderer performance for simple rectangular area (…
Browse files Browse the repository at this point in the history
…see BBoxAATileGenerator), added new statistics in Renderer
  • Loading branch information
bourgesl committed May 26, 2023
1 parent 4eb778d commit 48cad0a
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 93 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<groupId>org.marlin</groupId>
<artifactId>marlin</artifactId>
<packaging>jar</packaging>
<version>0.9.4.6-Unsafe</version>
<version>0.9.4.7-Unsafe</version>
<name>Marlin software rasterizer</name>

<url>https://github.com/bourgesl/marlin-renderer</url>
Expand Down
153 changes: 153 additions & 0 deletions src/main/java/sun/java2d/marlin/BBoxAATileGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, JetBrains s.r.o.. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package sun.java2d.marlin;

import sun.java2d.pipe.AATileGenerator;

class BBoxAATileGenerator implements AATileGenerator, MarlinConst {

protected static final boolean DISABLE_BLEND = false;

protected final Renderer renderer;
protected final MarlinCache cache;

// per-thread renderer stats
final RendererStats rdrStats;

BBoxAATileGenerator(final RendererStats stats, final Renderer r,
final MarlinCache cache)
{
this.rdrStats = stats;
this.renderer = r;
this.cache = cache;
}

/**
* Disposes this tile generator:
* clean up before reusing this instance
*/
@Override
public void dispose() {
if (DO_MONITORS) {
// called from AAShapePipe.renderTiles() (render tiles end):
rdrStats.mon_pipe_renderTiles.stop();
}
// dispose renderer and recycle the RendererContext instance:
renderer.dispose();
}

public final void getBbox(int[] bbox) {
bbox[0] = cache.bboxX0;
bbox[1] = cache.bboxY0;
bbox[2] = cache.bboxX1;
bbox[3] = cache.bboxY1;
}

/**
* Gets the width of the tiles that the generator batches output into.
* @return the width of the standard alpha tile
*/
@Override
public final int getTileWidth() {
if (DO_MONITORS) {
// called from AAShapePipe.renderTiles() (render tiles start):
rdrStats.mon_pipe_renderTiles.start();
}
return TILE_W;
}

/**
* Gets the height of the tiles that the generator batches output into.
* @return the height of the standard alpha tile
*/
@Override
public final int getTileHeight() {
return TILE_H;
}

/**
* Gets the typical alpha value that will characterize the current
* tile.
* The answer may be 0x00 to indicate that the current tile has
* no coverage in any of its pixels, or it may be 0xff to indicate
* that the current tile is completely covered by the path, or any
* other value to indicate non-trivial coverage cases.
* @return 0x00 for no coverage, 0xff for total coverage, or any other
* value for partial coverage of the tile
*/
@Override
public int getTypicalAlpha() {
if (DISABLE_BLEND) {
// always return empty tiles to disable blending operations
return 0x00;
}

// Note: if we have a filled rectangle that doesn't end on a tile
// border, we could still return 0xff, even though al!=maxTileAlphaSum
// This is because if we return 0xff, our users will fill a rectangle
// starting at x,y that has width = Math.min(TILE_SIZE, bboxX1-x),
// and height min(TILE_SIZE,bboxY1-y), which is what should happen.
// However, to support this, we would have to use 2 Math.min's
// and 2 multiplications per tile, instead of just 2 multiplications
// to compute maxTileAlphaSum. The savings offered would probably
// not be worth it, considering how rare this case is.
// Note: I have not tested this, so in the future if it is determined
// that it is worth it, it should be implemented. Perhaps this method's
// interface should be changed to take arguments the width and height
// of the current tile. This would eliminate the 2 Math.min calls that
// would be needed here, since our caller needs to compute these 2
// values anyway.
// this default implementation returns FULL tiles:
final int alpha = 0xff;
if (DO_STATS) {
rdrStats.hist_tile_generator_alpha.add(alpha);
}
return alpha;
}

/**
* Skips the current tile and moves on to the next tile.
* Either this method, or the getAlpha() method should be called
* once per tile, but not both.
*/
@Override
public void nextTile() {
// this default implementation does nothing
}

/**
* Gets the alpha coverage values for the current tile.
* Either this method, or the nextTile() method should be called
* once per tile, but not both.
*/
@Override
public void getAlpha(final byte[] tile, final int offset,
final int rowstride)
{
// this default implementation does nothing
}
}
16 changes: 7 additions & 9 deletions src/main/java/sun/java2d/marlin/DMarlinRenderingEngine.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -909,7 +909,7 @@ public AATileGenerator getAATileGenerator(Shape s,
boolean normalize,
int[] bbox)
{
MarlinTileGenerator ptg = null;
BBoxAATileGenerator ptg = null;
Renderer r = null;

final RendererContext rdrCtx = getRendererContext();
Expand Down Expand Up @@ -983,11 +983,10 @@ public AATileGenerator getAATileGenerator(Shape s,

strokeTo(rdrCtx, s, _at, bs, thin, norm, true, r);
}
if (r.endRendering()) {
ptg = rdrCtx.ptg.init();
if ((ptg = r.endRendering()) != null) {
ptg.getBbox(bbox);
// note: do not returnRendererContext(rdrCtx)
// as it will be called later by MarlinTileGenerator.dispose()
// as it will be called later by BBoxAATileGenerator.dispose()
r = null;
}
} finally {
Expand Down Expand Up @@ -1032,7 +1031,7 @@ public AATileGenerator getAATileGenerator(double x, double y,
ldx1 = ldy1 = ldx2 = ldy2 = 0.0d;
}

MarlinTileGenerator ptg = null;
BBoxAATileGenerator ptg = null;
Renderer r = null;

final RendererContext rdrCtx = getRendererContext();
Expand Down Expand Up @@ -1062,11 +1061,10 @@ public AATileGenerator getAATileGenerator(double x, double y,
}
r.pathDone();

if (r.endRendering()) {
ptg = rdrCtx.ptg.init();
if ((ptg = r.endRendering()) != null) {
ptg.getBbox(bbox);
// note: do not returnRendererContext(rdrCtx)
// as it will be called later by MarlinTileGenerator.dispose()
// as it will be called later by BBoxAATileGenerator.dispose()
r = null;
}
} finally {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/sun/java2d/marlin/MarlinCache.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -120,13 +120,17 @@ public final class MarlinCache implements MarlinConst {
tileMax = Integer.MIN_VALUE;
}

void init(int minx, int miny, int maxx, int maxy)
{
void initBBox(int minx, int miny, int maxx, int maxy) {
// assert maxy >= miny && maxx >= minx;
bboxX0 = minx;
bboxY0 = miny;
bboxX1 = maxx;
bboxY1 = maxy;
}

void init(int minx, int miny, int maxx, int maxy)
{
initBBox(minx, miny, maxx, maxy);

final int width = (maxx - minx);

Expand Down
50 changes: 4 additions & 46 deletions src/main/java/sun/java2d/marlin/MarlinTileGenerator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,9 +29,7 @@
import sun.java2d.pipe.AATileGenerator;
import sun.misc.Unsafe;

final class MarlinTileGenerator implements AATileGenerator, MarlinConst {

private static final boolean DISABLE_BLEND = false;
final class MarlinTileGenerator extends BBoxAATileGenerator {

private static final int MAX_TILE_ALPHA_SUM = TILE_W * TILE_H * MAX_AA_ALPHA;

Expand All @@ -52,19 +50,12 @@ final class MarlinTileGenerator implements AATileGenerator, MarlinConst {
}
}

private final Renderer renderer;
private final MarlinCache cache;
private int x, y;

// per-thread renderer stats
final RendererStats rdrStats;

MarlinTileGenerator(final RendererStats stats, final Renderer r,
final MarlinCache cache)
{
this.rdrStats = stats;
this.renderer = r;
this.cache = cache;
super(stats, r, cache);
}

MarlinTileGenerator init() {
Expand All @@ -80,43 +71,10 @@ MarlinTileGenerator init() {
*/
@Override
public void dispose() {
if (DO_MONITORS) {
// called from AAShapePipe.renderTiles() (render tiles end):
rdrStats.mon_pipe_renderTiles.stop();
}
// dispose cache:
cache.dispose();
// dispose renderer and recycle the RendererContext instance:
renderer.dispose();
}

void getBbox(int[] bbox) {
bbox[0] = cache.bboxX0;
bbox[1] = cache.bboxY0;
bbox[2] = cache.bboxX1;
bbox[3] = cache.bboxY1;
}

/**
* Gets the width of the tiles that the generator batches output into.
* @return the width of the standard alpha tile
*/
@Override
public int getTileWidth() {
if (DO_MONITORS) {
// called from AAShapePipe.renderTiles() (render tiles start):
rdrStats.mon_pipe_renderTiles.start();
}
return TILE_W;
}

/**
* Gets the height of the tiles that the generator batches output into.
* @return the height of the standard alpha tile
*/
@Override
public int getTileHeight() {
return TILE_H;
super.dispose();
}

/**
Expand Down
Loading

0 comments on commit 48cad0a

Please sign in to comment.