Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/marlin/Renderer.java
38918 views
/*1* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.marlin;2627import java.util.Arrays;28import sun.awt.geom.PathConsumer2D;29import static sun.java2d.marlin.OffHeapArray.SIZE_INT;30import sun.misc.Unsafe;3132final class Renderer implements PathConsumer2D, MarlinConst {3334static final boolean DISABLE_RENDER = false;3536static final boolean ENABLE_BLOCK_FLAGS = MarlinProperties.isUseTileFlags();37static final boolean ENABLE_BLOCK_FLAGS_HEURISTICS = MarlinProperties.isUseTileFlagsWithHeuristics();3839private static final int ALL_BUT_LSB = 0xfffffffe;40private static final int ERR_STEP_MAX = 0x7fffffff; // = 2^31 - 14142private static final double POWER_2_TO_32 = 0x1.0p32;4344// use float to make tosubpix methods faster (no int to float conversion)45public static final float f_SUBPIXEL_POSITIONS_X46= (float) SUBPIXEL_POSITIONS_X;47public static final float f_SUBPIXEL_POSITIONS_Y48= (float) SUBPIXEL_POSITIONS_Y;49public static final int SUBPIXEL_MASK_X = SUBPIXEL_POSITIONS_X - 1;50public static final int SUBPIXEL_MASK_Y = SUBPIXEL_POSITIONS_Y - 1;5152// number of subpixels corresponding to a tile line53private static final int SUBPIXEL_TILE54= TILE_SIZE << SUBPIXEL_LG_POSITIONS_Y;5556// 2048 (pixelSize) pixels (height) x 8 subpixels = 64K57static final int INITIAL_BUCKET_ARRAY58= INITIAL_PIXEL_DIM * SUBPIXEL_POSITIONS_Y;5960public static final int WIND_EVEN_ODD = 0;61public static final int WIND_NON_ZERO = 1;6263// common to all types of input path segments.64// OFFSET as bytes65// only integer values:66public static final long OFF_CURX_OR = 0;67public static final long OFF_ERROR = OFF_CURX_OR + SIZE_INT;68public static final long OFF_BUMP_X = OFF_ERROR + SIZE_INT;69public static final long OFF_BUMP_ERR = OFF_BUMP_X + SIZE_INT;70public static final long OFF_NEXT = OFF_BUMP_ERR + SIZE_INT;71public static final long OFF_YMAX = OFF_NEXT + SIZE_INT;7273// size of one edge in bytes74public static final int SIZEOF_EDGE_BYTES = (int)(OFF_YMAX + SIZE_INT);7576// curve break into lines77// cubic error in subpixels to decrement step78private static final float CUB_DEC_ERR_SUBPIX79= 2.5f * (NORM_SUBPIXELS / 8f); // 2.5 subpixel for typical 8x8 subpixels80// cubic error in subpixels to increment step81private static final float CUB_INC_ERR_SUBPIX82= 1f * (NORM_SUBPIXELS / 8f); // 1 subpixel for typical 8x8 subpixels8384// cubic bind length to decrement step = 8 * error in subpixels85// pisces: 20 / 886// openjfx pisces: 8 / 3.287// multiply by 8 = error scale factor:88public static final float CUB_DEC_BND89= 8f * CUB_DEC_ERR_SUBPIX; // 20f means 2.5 subpixel error90// cubic bind length to increment step = 8 * error in subpixels91public static final float CUB_INC_BND92= 8f * CUB_INC_ERR_SUBPIX; // 8f means 1 subpixel error9394// cubic countlg95public static final int CUB_COUNT_LG = 2;96// cubic count = 2^countlg97private static final int CUB_COUNT = 1 << CUB_COUNT_LG;98// cubic count^2 = 4^countlg99private static final int CUB_COUNT_2 = 1 << (2 * CUB_COUNT_LG);100// cubic count^3 = 8^countlg101private static final int CUB_COUNT_3 = 1 << (3 * CUB_COUNT_LG);102// cubic dt = 1 / count103private static final float CUB_INV_COUNT = 1f / CUB_COUNT;104// cubic dt^2 = 1 / count^2 = 1 / 4^countlg105private static final float CUB_INV_COUNT_2 = 1f / CUB_COUNT_2;106// cubic dt^3 = 1 / count^3 = 1 / 8^countlg107private static final float CUB_INV_COUNT_3 = 1f / CUB_COUNT_3;108109// quad break into lines110// quadratic error in subpixels111private static final float QUAD_DEC_ERR_SUBPIX112= 1f * (NORM_SUBPIXELS / 8f); // 1 subpixel for typical 8x8 subpixels113114// quadratic bind length to decrement step = 8 * error in subpixels115// pisces and openjfx pisces: 32116public static final float QUAD_DEC_BND117= 8f * QUAD_DEC_ERR_SUBPIX; // 8f means 1 subpixel error118119//////////////////////////////////////////////////////////////////////////////120// SCAN LINE121//////////////////////////////////////////////////////////////////////////////122// crossings ie subpixel edge x coordinates123private int[] crossings;124// auxiliary storage for crossings (merge sort)125private int[] aux_crossings;126127// indices into the segment pointer lists. They indicate the "active"128// sublist in the segment lists (the portion of the list that contains129// all the segments that cross the next scan line).130private int edgeCount;131private int[] edgePtrs;132// auxiliary storage for edge pointers (merge sort)133private int[] aux_edgePtrs;134135// max used for both edgePtrs and crossings (stats only)136private int activeEdgeMaxUsed;137138// per-thread initial arrays (large enough to satisfy most usages) (1024)139private final int[] crossings_initial = new int[INITIAL_SMALL_ARRAY]; // 4K140// +1 to avoid recycling in Helpers.widenArray()141private final int[] edgePtrs_initial = new int[INITIAL_SMALL_ARRAY + 1]; // 4K142// merge sort initial arrays (large enough to satisfy most usages) (1024)143private final int[] aux_crossings_initial = new int[INITIAL_SMALL_ARRAY]; // 4K144// +1 to avoid recycling in Helpers.widenArray()145private final int[] aux_edgePtrs_initial = new int[INITIAL_SMALL_ARRAY + 1]; // 4K146147//////////////////////////////////////////////////////////////////////////////148// EDGE LIST149//////////////////////////////////////////////////////////////////////////////150private int edgeMinY = Integer.MAX_VALUE;151private int edgeMaxY = Integer.MIN_VALUE;152private float edgeMinX = Float.POSITIVE_INFINITY;153private float edgeMaxX = Float.NEGATIVE_INFINITY;154155// edges [floats|ints] stored in off-heap memory156private final OffHeapArray edges;157158private int[] edgeBuckets;159private int[] edgeBucketCounts; // 2*newedges + (1 if pruning needed)160// used range for edgeBuckets / edgeBucketCounts161private int buckets_minY;162private int buckets_maxY;163// sum of each edge delta Y (subpixels)164private int edgeSumDeltaY;165166// +1 to avoid recycling in Helpers.widenArray()167private final int[] edgeBuckets_initial168= new int[INITIAL_BUCKET_ARRAY + 1]; // 64K169private final int[] edgeBucketCounts_initial170= new int[INITIAL_BUCKET_ARRAY + 1]; // 64K171172// Flattens using adaptive forward differencing. This only carries out173// one iteration of the AFD loop. All it does is update AFD variables (i.e.174// X0, Y0, D*[X|Y], COUNT; not variables used for computing scanline crossings).175private void quadBreakIntoLinesAndAdd(float x0, float y0,176final Curve c,177final float x2, final float y2)178{179int count = 1; // dt = 1 / count180181// maximum(ddX|Y) = norm(dbx, dby) * dt^2 (= 1)182float maxDD = FloatMath.max(Math.abs(c.dbx), Math.abs(c.dby));183184final float _DEC_BND = QUAD_DEC_BND;185186while (maxDD >= _DEC_BND) {187// divide step by half:188maxDD /= 4f; // error divided by 2^2 = 4189190count <<= 1;191if (doStats) {192RendererContext.stats.stat_rdr_quadBreak_dec.add(count);193}194}195196int nL = 0; // line count197if (count > 1) {198final float icount = 1f / count; // dt199final float icount2 = icount * icount; // dt^2200201final float ddx = c.dbx * icount2;202final float ddy = c.dby * icount2;203float dx = c.bx * icount2 + c.cx * icount;204float dy = c.by * icount2 + c.cy * icount;205206float x1, y1;207208while (--count > 0) {209x1 = x0 + dx;210dx += ddx;211y1 = y0 + dy;212dy += ddy;213214addLine(x0, y0, x1, y1);215216if (doStats) { nL++; }217x0 = x1;218y0 = y1;219}220}221addLine(x0, y0, x2, y2);222223if (doStats) {224RendererContext.stats.stat_rdr_quadBreak.add(nL + 1);225}226}227228// x0, y0 and x3,y3 are the endpoints of the curve. We could compute these229// using c.xat(0),c.yat(0) and c.xat(1),c.yat(1), but this might introduce230// numerical errors, and our callers already have the exact values.231// Another alternative would be to pass all the control points, and call232// c.set here, but then too many numbers are passed around.233private void curveBreakIntoLinesAndAdd(float x0, float y0,234final Curve c,235final float x3, final float y3)236{237int count = CUB_COUNT;238final float icount = CUB_INV_COUNT; // dt239final float icount2 = CUB_INV_COUNT_2; // dt^2240final float icount3 = CUB_INV_COUNT_3; // dt^3241242// the dx and dy refer to forward differencing variables, not the last243// coefficients of the "points" polynomial244float dddx, dddy, ddx, ddy, dx, dy;245dddx = 2f * c.dax * icount3;246dddy = 2f * c.day * icount3;247ddx = dddx + c.dbx * icount2;248ddy = dddy + c.dby * icount2;249dx = c.ax * icount3 + c.bx * icount2 + c.cx * icount;250dy = c.ay * icount3 + c.by * icount2 + c.cy * icount;251252// we use x0, y0 to walk the line253float x1 = x0, y1 = y0;254int nL = 0; // line count255256final float _DEC_BND = CUB_DEC_BND;257final float _INC_BND = CUB_INC_BND;258259while (count > 0) {260// divide step by half:261while (Math.abs(ddx) >= _DEC_BND || Math.abs(ddy) >= _DEC_BND) {262dddx /= 8f;263dddy /= 8f;264ddx = ddx/4f - dddx;265ddy = ddy/4f - dddy;266dx = (dx - ddx) / 2f;267dy = (dy - ddy) / 2f;268269count <<= 1;270if (doStats) {271RendererContext.stats.stat_rdr_curveBreak_dec.add(count);272}273}274275// double step:276// TODO: why use first derivative dX|Y instead of second ddX|Y ?277// both scale changes should use speed or acceleration to have the same metric.278279// can only do this on even "count" values, because we must divide count by 2280while (count % 2 == 0281&& Math.abs(dx) <= _INC_BND && Math.abs(dy) <= _INC_BND)282{283dx = 2f * dx + ddx;284dy = 2f * dy + ddy;285ddx = 4f * (ddx + dddx);286ddy = 4f * (ddy + dddy);287dddx *= 8f;288dddy *= 8f;289290count >>= 1;291if (doStats) {292RendererContext.stats.stat_rdr_curveBreak_inc.add(count);293}294}295if (--count > 0) {296x1 += dx;297dx += ddx;298ddx += dddx;299y1 += dy;300dy += ddy;301ddy += dddy;302} else {303x1 = x3;304y1 = y3;305}306307addLine(x0, y0, x1, y1);308309if (doStats) { nL++; }310x0 = x1;311y0 = y1;312}313if (doStats) {314RendererContext.stats.stat_rdr_curveBreak.add(nL);315}316}317318private void addLine(float x1, float y1, float x2, float y2) {319if (doMonitors) {320RendererContext.stats.mon_rdr_addLine.start();321}322if (doStats) {323RendererContext.stats.stat_rdr_addLine.add(1);324}325int or = 1; // orientation of the line. 1 if y increases, 0 otherwise.326if (y2 < y1) {327or = 0;328float tmp = y2;329y2 = y1;330y1 = tmp;331tmp = x2;332x2 = x1;333x1 = tmp;334}335336// convert subpixel coordinates (float) into pixel positions (int)337338// The index of the pixel that holds the next HPC is at ceil(trueY - 0.5)339// Since y1 and y2 are biased by -0.5 in tosubpixy(), this is simply340// ceil(y1) or ceil(y2)341// upper integer (inclusive)342final int firstCrossing = FloatMath.max(FloatMath.ceil_int(y1), boundsMinY);343344// note: use boundsMaxY (last Y exclusive) to compute correct coverage345// upper integer (exclusive)346final int lastCrossing = FloatMath.min(FloatMath.ceil_int(y2), boundsMaxY);347348/* skip horizontal lines in pixel space and clip edges349out of y range [boundsMinY; boundsMaxY] */350if (firstCrossing >= lastCrossing) {351if (doMonitors) {352RendererContext.stats.mon_rdr_addLine.stop();353}354if (doStats) {355RendererContext.stats.stat_rdr_addLine_skip.add(1);356}357return;358}359360// edge min/max X/Y are in subpixel space (inclusive) within bounds:361// note: Use integer crossings to ensure consistent range within362// edgeBuckets / edgeBucketCounts arrays in case of NaN values (int = 0)363if (firstCrossing < edgeMinY) {364edgeMinY = firstCrossing;365}366if (lastCrossing > edgeMaxY) {367edgeMaxY = lastCrossing;368}369370// Use double-precision for improved accuracy:371final double x1d = x1;372final double y1d = y1;373final double slope = (x1d - x2) / (y1d - y2);374375if (slope >= 0.0) { // <==> x1 < x2376if (x1 < edgeMinX) {377edgeMinX = x1;378}379if (x2 > edgeMaxX) {380edgeMaxX = x2;381}382} else {383if (x2 < edgeMinX) {384edgeMinX = x2;385}386if (x1 > edgeMaxX) {387edgeMaxX = x1;388}389}390391// local variables for performance:392final int _SIZEOF_EDGE_BYTES = SIZEOF_EDGE_BYTES;393394final OffHeapArray _edges = edges;395396// get free pointer (ie length in bytes)397final int edgePtr = _edges.used;398399// use substraction to avoid integer overflow:400if (_edges.length - edgePtr < _SIZEOF_EDGE_BYTES) {401// suppose _edges.length > _SIZEOF_EDGE_BYTES402// so doubling size is enough to add needed bytes403// note: throw IOOB if neededSize > 2Gb:404final long edgeNewSize = ArrayCache.getNewLargeSize(_edges.length,405edgePtr + _SIZEOF_EDGE_BYTES);406407if (doStats) {408RendererContext.stats.stat_rdr_edges_resizes.add(edgeNewSize);409}410_edges.resize(edgeNewSize);411}412413414final Unsafe _unsafe = OffHeapArray.unsafe;415final long SIZE_INT = 4L;416long addr = _edges.address + edgePtr;417418// The x value must be bumped up to its position at the next HPC we will evaluate.419// "firstcrossing" is the (sub)pixel number where the next crossing occurs420// thus, the actual coordinate of the next HPC is "firstcrossing + 0.5"421// so the Y distance we cover is "firstcrossing + 0.5 - trueY".422// Note that since y1 (and y2) are already biased by -0.5 in tosubpixy(), we have423// y1 = trueY - 0.5424// trueY = y1 + 0.5425// firstcrossing + 0.5 - trueY = firstcrossing + 0.5 - (y1 + 0.5)426// = firstcrossing - y1427// The x coordinate at that HPC is then:428// x1_intercept = x1 + (firstcrossing - y1) * slope429// The next VPC is then given by:430// VPC index = ceil(x1_intercept - 0.5), or alternately431// VPC index = floor(x1_intercept - 0.5 + 1 - epsilon)432// epsilon is hard to pin down in floating point, but easy in fixed point, so if433// we convert to fixed point then these operations get easier:434// long x1_fixed = x1_intercept * 2^32; (fixed point 32.32 format)435// curx = next VPC = fixed_floor(x1_fixed - 2^31 + 2^32 - 1)436// = fixed_floor(x1_fixed + 2^31 - 1)437// = fixed_floor(x1_fixed + 0x7fffffff)438// and error = fixed_fract(x1_fixed + 0x7fffffff)439final double x1_intercept = x1d + (firstCrossing - y1d) * slope;440441// inlined scalb(x1_intercept, 32):442final long x1_fixed_biased = ((long) (POWER_2_TO_32 * x1_intercept))443+ 0x7fffffffL;444// curx:445// last bit corresponds to the orientation446_unsafe.putInt(addr, (((int) (x1_fixed_biased >> 31L)) & ALL_BUT_LSB) | or);447addr += SIZE_INT;448_unsafe.putInt(addr, ((int) x1_fixed_biased) >>> 1);449addr += SIZE_INT;450451// inlined scalb(slope, 32):452final long slope_fixed = (long) (POWER_2_TO_32 * slope);453454// last bit set to 0 to keep orientation:455_unsafe.putInt(addr, (((int) (slope_fixed >> 31L)) & ALL_BUT_LSB));456addr += SIZE_INT;457_unsafe.putInt(addr, ((int) slope_fixed) >>> 1);458addr += SIZE_INT;459460final int[] _edgeBuckets = edgeBuckets;461final int[] _edgeBucketCounts = edgeBucketCounts;462463final int _boundsMinY = boundsMinY;464465// each bucket is a linked list. this method adds ptr to the466// start of the "bucket"th linked list.467final int bucketIdx = firstCrossing - _boundsMinY;468469// pointer from bucket470_unsafe.putInt(addr, _edgeBuckets[bucketIdx]);471addr += SIZE_INT;472// y max (inclusive)473_unsafe.putInt(addr, lastCrossing);474475// Update buckets:476// directly the edge struct "pointer"477_edgeBuckets[bucketIdx] = edgePtr;478_edgeBucketCounts[bucketIdx] += 2; // 1 << 1479// last bit means edge end480_edgeBucketCounts[lastCrossing - _boundsMinY] |= 0x1;481482// update sum of delta Y (subpixels):483edgeSumDeltaY += (lastCrossing - firstCrossing);484485// update free pointer (ie length in bytes)486_edges.used += _SIZEOF_EDGE_BYTES;487488if (doMonitors) {489RendererContext.stats.mon_rdr_addLine.stop();490}491}492493// END EDGE LIST494//////////////////////////////////////////////////////////////////////////////495496// Cache to store RLE-encoded coverage mask of the current primitive497final MarlinCache cache;498499// Bounds of the drawing region, at subpixel precision.500private int boundsMinX, boundsMinY, boundsMaxX, boundsMaxY;501502// Current winding rule503private int windingRule;504505// Current drawing position, i.e., final point of last segment506private float x0, y0;507508// Position of most recent 'moveTo' command509private float sx0, sy0;510511// per-thread renderer context512final RendererContext rdrCtx;513// dirty curve514private final Curve curve;515516Renderer(final RendererContext rdrCtx) {517this.rdrCtx = rdrCtx;518519this.edges = new OffHeapArray(rdrCtx, INITIAL_EDGES_CAPACITY); // 96K520521this.curve = rdrCtx.curve;522523edgeBuckets = edgeBuckets_initial;524edgeBucketCounts = edgeBucketCounts_initial;525526alphaLine = alphaLine_initial;527528this.cache = rdrCtx.cache;529530// ScanLine:531crossings = crossings_initial;532aux_crossings = aux_crossings_initial;533edgePtrs = edgePtrs_initial;534aux_edgePtrs = aux_edgePtrs_initial;535536edgeCount = 0;537activeEdgeMaxUsed = 0;538}539540Renderer init(final int pix_boundsX, final int pix_boundsY,541final int pix_boundsWidth, final int pix_boundsHeight,542final int windingRule) {543544this.windingRule = windingRule;545546// bounds as half-open intervals: minX <= x < maxX and minY <= y < maxY547this.boundsMinX = pix_boundsX << SUBPIXEL_LG_POSITIONS_X;548this.boundsMaxX =549(pix_boundsX + pix_boundsWidth) << SUBPIXEL_LG_POSITIONS_X;550this.boundsMinY = pix_boundsY << SUBPIXEL_LG_POSITIONS_Y;551this.boundsMaxY =552(pix_boundsY + pix_boundsHeight) << SUBPIXEL_LG_POSITIONS_Y;553554if (doLogBounds) {555MarlinUtils.logInfo("boundsXY = [" + boundsMinX + " ... "556+ boundsMaxX + "[ [" + boundsMinY + " ... "557+ boundsMaxY + "[");558}559560// see addLine: ceil(boundsMaxY) => boundsMaxY + 1561// +1 for edgeBucketCounts562final int edgeBucketsLength = (boundsMaxY - boundsMinY) + 1;563564if (edgeBucketsLength > INITIAL_BUCKET_ARRAY) {565if (doStats) {566RendererContext.stats.stat_array_renderer_edgeBuckets567.add(edgeBucketsLength);568RendererContext.stats.stat_array_renderer_edgeBucketCounts569.add(edgeBucketsLength);570}571edgeBuckets = rdrCtx.getIntArray(edgeBucketsLength);572edgeBucketCounts = rdrCtx.getIntArray(edgeBucketsLength);573}574575edgeMinY = Integer.MAX_VALUE;576edgeMaxY = Integer.MIN_VALUE;577edgeMinX = Float.POSITIVE_INFINITY;578edgeMaxX = Float.NEGATIVE_INFINITY;579580// reset used mark:581edgeCount = 0;582activeEdgeMaxUsed = 0;583edges.used = 0;584585edgeSumDeltaY = 0;586587return this; // fluent API588}589590/**591* Disposes this renderer and recycle it clean up before reusing this instance592*/593void dispose() {594if (doStats) {595RendererContext.stats.stat_rdr_activeEdges.add(activeEdgeMaxUsed);596RendererContext.stats.stat_rdr_edges.add(edges.used);597RendererContext.stats.stat_rdr_edges_count598.add(edges.used / SIZEOF_EDGE_BYTES);599}600if (doCleanDirty) {601// Force zero-fill dirty arrays:602Arrays.fill(crossings, 0);603Arrays.fill(aux_crossings, 0);604Arrays.fill(edgePtrs, 0);605Arrays.fill(aux_edgePtrs, 0);606}607// Return arrays:608if (crossings != crossings_initial) {609rdrCtx.putDirtyIntArray(crossings);610crossings = crossings_initial;611if (aux_crossings != aux_crossings_initial) {612rdrCtx.putDirtyIntArray(aux_crossings);613aux_crossings = aux_crossings_initial;614}615}616if (edgePtrs != edgePtrs_initial) {617rdrCtx.putDirtyIntArray(edgePtrs);618edgePtrs = edgePtrs_initial;619if (aux_edgePtrs != aux_edgePtrs_initial) {620rdrCtx.putDirtyIntArray(aux_edgePtrs);621aux_edgePtrs = aux_edgePtrs_initial;622}623}624if (alphaLine != alphaLine_initial) {625rdrCtx.putIntArray(alphaLine, 0, 0); // already zero filled626alphaLine = alphaLine_initial;627}628if (blkFlags != blkFlags_initial) {629rdrCtx.putIntArray(blkFlags, 0, 0); // already zero filled630blkFlags = blkFlags_initial;631}632633if (edgeMinY != Integer.MAX_VALUE) {634// if context is maked as DIRTY:635if (rdrCtx.dirty) {636// may happen if an exception if thrown in the pipeline processing:637// clear completely buckets arrays:638buckets_minY = 0;639buckets_maxY = boundsMaxY - boundsMinY;640}641// clear used part642if (edgeBuckets == edgeBuckets_initial) {643// fill only used part644IntArrayCache.fill(edgeBuckets, buckets_minY,645buckets_maxY, 0);646IntArrayCache.fill(edgeBucketCounts, buckets_minY,647buckets_maxY + 1, 0);648} else {649// clear only used part650rdrCtx.putIntArray(edgeBuckets, buckets_minY,651buckets_maxY);652edgeBuckets = edgeBuckets_initial;653654rdrCtx.putIntArray(edgeBucketCounts, buckets_minY,655buckets_maxY + 1);656edgeBucketCounts = edgeBucketCounts_initial;657}658} else if (edgeBuckets != edgeBuckets_initial) {659// unused arrays660rdrCtx.putIntArray(edgeBuckets, 0, 0);661edgeBuckets = edgeBuckets_initial;662663rdrCtx.putIntArray(edgeBucketCounts, 0, 0);664edgeBucketCounts = edgeBucketCounts_initial;665}666667// At last: resize back off-heap edges to initial size668if (edges.length != INITIAL_EDGES_CAPACITY) {669// note: may throw OOME:670edges.resize(INITIAL_EDGES_CAPACITY);671}672if (doCleanDirty) {673// Force zero-fill dirty arrays:674edges.fill(BYTE_0);675}676if (doMonitors) {677RendererContext.stats.mon_rdr_endRendering.stop();678}679}680681private static float tosubpixx(final float pix_x) {682return f_SUBPIXEL_POSITIONS_X * pix_x;683}684685private static float tosubpixy(final float pix_y) {686// shift y by -0.5 for fast ceil(y - 0.5):687return f_SUBPIXEL_POSITIONS_Y * pix_y - 0.5f;688}689690@Override691public void moveTo(float pix_x0, float pix_y0) {692closePath();693final float sx = tosubpixx(pix_x0);694final float sy = tosubpixy(pix_y0);695this.sx0 = sx;696this.sy0 = sy;697this.x0 = sx;698this.y0 = sy;699}700701@Override702public void lineTo(float pix_x1, float pix_y1) {703final float x1 = tosubpixx(pix_x1);704final float y1 = tosubpixy(pix_y1);705addLine(x0, y0, x1, y1);706x0 = x1;707y0 = y1;708}709710@Override711public void curveTo(float x1, float y1,712float x2, float y2,713float x3, float y3)714{715final float xe = tosubpixx(x3);716final float ye = tosubpixy(y3);717curve.set(x0, y0, tosubpixx(x1), tosubpixy(y1),718tosubpixx(x2), tosubpixy(y2), xe, ye);719curveBreakIntoLinesAndAdd(x0, y0, curve, xe, ye);720x0 = xe;721y0 = ye;722}723724@Override725public void quadTo(float x1, float y1, float x2, float y2) {726final float xe = tosubpixx(x2);727final float ye = tosubpixy(y2);728curve.set(x0, y0, tosubpixx(x1), tosubpixy(y1), xe, ye);729quadBreakIntoLinesAndAdd(x0, y0, curve, xe, ye);730x0 = xe;731y0 = ye;732}733734@Override735public void closePath() {736addLine(x0, y0, sx0, sy0);737x0 = sx0;738y0 = sy0;739}740741@Override742public void pathDone() {743closePath();744}745746@Override747public long getNativeConsumer() {748throw new InternalError("Renderer does not use a native consumer.");749}750751// clean alpha array (zero filled)752private int[] alphaLine;753// 2048 (pixelsize) pixel large754private final int[] alphaLine_initial = new int[INITIAL_AA_ARRAY]; // 8K755756private void _endRendering(final int ymin, final int ymax) {757if (DISABLE_RENDER) {758return;759}760761// Get X bounds as true pixel boundaries to compute correct pixel coverage:762final int bboxx0 = bbox_spminX;763final int bboxx1 = bbox_spmaxX;764765final boolean windingRuleEvenOdd = (windingRule == WIND_EVEN_ODD);766767// Useful when processing tile line by tile line768final int[] _alpha = alphaLine;769770// local vars (performance):771final MarlinCache _cache = cache;772final OffHeapArray _edges = edges;773final int[] _edgeBuckets = edgeBuckets;774final int[] _edgeBucketCounts = edgeBucketCounts;775776int[] _crossings = this.crossings;777int[] _edgePtrs = this.edgePtrs;778779// merge sort auxiliary storage:780int[] _aux_crossings = this.aux_crossings;781int[] _aux_edgePtrs = this.aux_edgePtrs;782783// copy constants:784final long _OFF_ERROR = OFF_ERROR;785final long _OFF_BUMP_X = OFF_BUMP_X;786final long _OFF_BUMP_ERR = OFF_BUMP_ERR;787788final long _OFF_NEXT = OFF_NEXT;789final long _OFF_YMAX = OFF_YMAX;790791final int _ALL_BUT_LSB = ALL_BUT_LSB;792final int _ERR_STEP_MAX = ERR_STEP_MAX;793794// unsafe I/O:795final Unsafe _unsafe = OffHeapArray.unsafe;796final long addr0 = _edges.address;797long addr;798final int _SUBPIXEL_LG_POSITIONS_X = SUBPIXEL_LG_POSITIONS_X;799final int _SUBPIXEL_LG_POSITIONS_Y = SUBPIXEL_LG_POSITIONS_Y;800final int _SUBPIXEL_MASK_X = SUBPIXEL_MASK_X;801final int _SUBPIXEL_MASK_Y = SUBPIXEL_MASK_Y;802final int _SUBPIXEL_POSITIONS_X = SUBPIXEL_POSITIONS_X;803804final int _MIN_VALUE = Integer.MIN_VALUE;805final int _MAX_VALUE = Integer.MAX_VALUE;806807// Now we iterate through the scanlines. We must tell emitRow the coord808// of the first non-transparent pixel, so we must keep accumulators for809// the first and last pixels of the section of the current pixel row810// that we will emit.811// We also need to accumulate pix_bbox, but the iterator does it812// for us. We will just get the values from it once this loop is done813int minX = _MAX_VALUE;814int maxX = _MIN_VALUE;815816int y = ymin;817int bucket = y - boundsMinY;818819int numCrossings = this.edgeCount;820int edgePtrsLen = _edgePtrs.length;821int crossingsLen = _crossings.length;822int _arrayMaxUsed = activeEdgeMaxUsed;823int ptrLen = 0, newCount, ptrEnd;824825int bucketcount, i, j, ecur;826int cross, lastCross;827int x0, x1, tmp, sum, prev, curx, curxo, crorientation, err;828int pix_x, pix_xmaxm1, pix_xmax;829830int low, high, mid, prevNumCrossings;831boolean useBinarySearch;832833final int[] _blkFlags = blkFlags;834final int _BLK_SIZE_LG = BLOCK_SIZE_LG;835final int _BLK_SIZE = BLOCK_SIZE;836837final boolean _enableBlkFlagsHeuristics = ENABLE_BLOCK_FLAGS_HEURISTICS && this.enableBlkFlags;838839// Use block flags if large pixel span and few crossings:840// ie mean(distance between crossings) is high841boolean useBlkFlags = this.prevUseBlkFlags;842843final int stroking = rdrCtx.stroking;844845int lastY = -1; // last emited row846847848// Iteration on scanlines849for (; y < ymax; y++, bucket++) {850// --- from former ScanLineIterator.next()851bucketcount = _edgeBucketCounts[bucket];852853// marker on previously sorted edges:854prevNumCrossings = numCrossings;855856// bucketCount indicates new edge / edge end:857if (bucketcount != 0) {858if (doStats) {859RendererContext.stats.stat_rdr_activeEdges_updates860.add(numCrossings);861}862863// last bit set to 1 means that edges ends864if ((bucketcount & 0x1) != 0) {865// eviction in active edge list866// cache edges[] address + offset867addr = addr0 + _OFF_YMAX;868869for (i = 0, newCount = 0; i < numCrossings; i++) {870// get the pointer to the edge871ecur = _edgePtrs[i];872// random access so use unsafe:873if (_unsafe.getInt(addr + ecur) > y) {874_edgePtrs[newCount++] = ecur;875}876}877// update marker on sorted edges minus removed edges:878prevNumCrossings = numCrossings = newCount;879}880881ptrLen = bucketcount >> 1; // number of new edge882883if (ptrLen != 0) {884if (doStats) {885RendererContext.stats.stat_rdr_activeEdges_adds886.add(ptrLen);887if (ptrLen > 10) {888RendererContext.stats.stat_rdr_activeEdges_adds_high889.add(ptrLen);890}891}892ptrEnd = numCrossings + ptrLen;893894if (edgePtrsLen < ptrEnd) {895if (doStats) {896RendererContext.stats.stat_array_renderer_edgePtrs897.add(ptrEnd);898}899this.edgePtrs = _edgePtrs900= rdrCtx.widenDirtyIntArray(_edgePtrs, numCrossings,901ptrEnd);902903edgePtrsLen = _edgePtrs.length;904// Get larger auxiliary storage:905if (_aux_edgePtrs != aux_edgePtrs_initial) {906rdrCtx.putDirtyIntArray(_aux_edgePtrs);907}908// use ArrayCache.getNewSize() to use the same growing909// factor than widenDirtyIntArray():910if (doStats) {911RendererContext.stats.stat_array_renderer_aux_edgePtrs912.add(ptrEnd);913}914this.aux_edgePtrs = _aux_edgePtrs915= rdrCtx.getDirtyIntArray(916ArrayCache.getNewSize(numCrossings, ptrEnd)917);918}919920// cache edges[] address + offset921addr = addr0 + _OFF_NEXT;922923// add new edges to active edge list:924for (ecur = _edgeBuckets[bucket];925numCrossings < ptrEnd; numCrossings++)926{927// store the pointer to the edge928_edgePtrs[numCrossings] = ecur;929// random access so use unsafe:930ecur = _unsafe.getInt(addr + ecur);931}932933if (crossingsLen < numCrossings) {934// Get larger array:935if (_crossings != crossings_initial) {936rdrCtx.putDirtyIntArray(_crossings);937}938if (doStats) {939RendererContext.stats.stat_array_renderer_crossings940.add(numCrossings);941}942this.crossings = _crossings943= rdrCtx.getDirtyIntArray(numCrossings);944945// Get larger auxiliary storage:946if (_aux_crossings != aux_crossings_initial) {947rdrCtx.putDirtyIntArray(_aux_crossings);948}949if (doStats) {950RendererContext.stats.stat_array_renderer_aux_crossings951.add(numCrossings);952}953this.aux_crossings = _aux_crossings954= rdrCtx.getDirtyIntArray(numCrossings);955956crossingsLen = _crossings.length;957}958if (doStats) {959// update max used mark960if (numCrossings > _arrayMaxUsed) {961_arrayMaxUsed = numCrossings;962}963}964} // ptrLen != 0965} // bucketCount != 0966967968if (numCrossings != 0) {969/*970* thresholds to switch to optimized merge sort971* for newly added edges + final merge pass.972*/973if ((ptrLen < 10) || (numCrossings < 40)) {974if (doStats) {975RendererContext.stats.hist_rdr_crossings976.add(numCrossings);977RendererContext.stats.hist_rdr_crossings_adds978.add(ptrLen);979}980981/*982* threshold to use binary insertion sort instead of983* straight insertion sort (to reduce minimize comparisons).984*/985useBinarySearch = (numCrossings >= 20);986987// if small enough:988lastCross = _MIN_VALUE;989990for (i = 0; i < numCrossings; i++) {991// get the pointer to the edge992ecur = _edgePtrs[i];993994/* convert subpixel coordinates (float) into pixel995positions (int) for coming scanline */996/* note: it is faster to always update edges even997if it is removed from AEL for coming or last scanline */998999// random access so use unsafe:1000addr = addr0 + ecur; // ecur + OFF_F_CURX10011002// get current crossing:1003curx = _unsafe.getInt(addr);10041005// update crossing with orientation at last bit:1006cross = curx;10071008// Increment x using DDA (fixed point):1009curx += _unsafe.getInt(addr + _OFF_BUMP_X);10101011// Increment error:1012err = _unsafe.getInt(addr + _OFF_ERROR)1013+ _unsafe.getInt(addr + _OFF_BUMP_ERR);10141015// Manual carry handling:1016// keep sign and carry bit only and ignore last bit (preserve orientation):1017_unsafe.putInt(addr, curx - ((err >> 30) & _ALL_BUT_LSB));1018_unsafe.putInt(addr + _OFF_ERROR, (err & _ERR_STEP_MAX));10191020if (doStats) {1021RendererContext.stats.stat_rdr_crossings_updates1022.add(numCrossings);1023}10241025// insertion sort of crossings:1026if (cross < lastCross) {1027if (doStats) {1028RendererContext.stats.stat_rdr_crossings_sorts1029.add(i);1030}10311032/* use binary search for newly added edges1033in crossings if arrays are large enough */1034if (useBinarySearch && (i >= prevNumCrossings)) {1035if (doStats) {1036RendererContext.stats.1037stat_rdr_crossings_bsearch.add(i);1038}1039low = 0;1040high = i - 1;10411042do {1043// note: use signed shift (not >>>) for performance1044// as indices are small enough to exceed Integer.MAX_VALUE1045mid = (low + high) >> 1;10461047if (_crossings[mid] < cross) {1048low = mid + 1;1049} else {1050high = mid - 1;1051}1052} while (low <= high);10531054for (j = i - 1; j >= low; j--) {1055_crossings[j + 1] = _crossings[j];1056_edgePtrs [j + 1] = _edgePtrs[j];1057}1058_crossings[low] = cross;1059_edgePtrs [low] = ecur;10601061} else {1062j = i - 1;1063_crossings[i] = _crossings[j];1064_edgePtrs[i] = _edgePtrs[j];10651066while ((--j >= 0) && (_crossings[j] > cross)) {1067_crossings[j + 1] = _crossings[j];1068_edgePtrs [j + 1] = _edgePtrs[j];1069}1070_crossings[j + 1] = cross;1071_edgePtrs [j + 1] = ecur;1072}10731074} else {1075_crossings[i] = lastCross = cross;1076}1077}1078} else {1079if (doStats) {1080RendererContext.stats.stat_rdr_crossings_msorts1081.add(numCrossings);1082RendererContext.stats.hist_rdr_crossings_ratio1083.add((1000 * ptrLen) / numCrossings);1084RendererContext.stats.hist_rdr_crossings_msorts1085.add(numCrossings);1086RendererContext.stats.hist_rdr_crossings_msorts_adds1087.add(ptrLen);1088}10891090// Copy sorted data in auxiliary arrays1091// and perform insertion sort on almost sorted data1092// (ie i < prevNumCrossings):10931094lastCross = _MIN_VALUE;10951096for (i = 0; i < numCrossings; i++) {1097// get the pointer to the edge1098ecur = _edgePtrs[i];10991100/* convert subpixel coordinates (float) into pixel1101positions (int) for coming scanline */1102/* note: it is faster to always update edges even1103if it is removed from AEL for coming or last scanline */11041105// random access so use unsafe:1106addr = addr0 + ecur; // ecur + OFF_F_CURX11071108// get current crossing:1109curx = _unsafe.getInt(addr);11101111// update crossing with orientation at last bit:1112cross = curx;11131114// Increment x using DDA (fixed point):1115curx += _unsafe.getInt(addr + _OFF_BUMP_X);11161117// Increment error:1118err = _unsafe.getInt(addr + _OFF_ERROR)1119+ _unsafe.getInt(addr + _OFF_BUMP_ERR);11201121// Manual carry handling:1122// keep sign and carry bit only and ignore last bit (preserve orientation):1123_unsafe.putInt(addr, curx - ((err >> 30) & _ALL_BUT_LSB));1124_unsafe.putInt(addr + _OFF_ERROR, (err & _ERR_STEP_MAX));11251126if (doStats) {1127RendererContext.stats.stat_rdr_crossings_updates1128.add(numCrossings);1129}11301131if (i >= prevNumCrossings) {1132// simply store crossing as edgePtrs is in-place:1133// will be copied and sorted efficiently by mergesort later:1134_crossings[i] = cross;11351136} else if (cross < lastCross) {1137if (doStats) {1138RendererContext.stats.stat_rdr_crossings_sorts1139.add(i);1140}11411142// (straight) insertion sort of crossings:1143j = i - 1;1144_aux_crossings[i] = _aux_crossings[j];1145_aux_edgePtrs[i] = _aux_edgePtrs[j];11461147while ((--j >= 0) && (_aux_crossings[j] > cross)) {1148_aux_crossings[j + 1] = _aux_crossings[j];1149_aux_edgePtrs [j + 1] = _aux_edgePtrs[j];1150}1151_aux_crossings[j + 1] = cross;1152_aux_edgePtrs [j + 1] = ecur;11531154} else {1155// auxiliary storage:1156_aux_crossings[i] = lastCross = cross;1157_aux_edgePtrs [i] = ecur;1158}1159}11601161// use Mergesort using auxiliary arrays (sort only right part)1162MergeSort.mergeSortNoCopy(_crossings, _edgePtrs,1163_aux_crossings, _aux_edgePtrs,1164numCrossings, prevNumCrossings);1165}11661167// reset ptrLen1168ptrLen = 0;1169// --- from former ScanLineIterator.next()117011711172/* note: bboxx0 and bboxx1 must be pixel boundaries1173to have correct coverage computation */11741175// right shift on crossings to get the x-coordinate:1176curxo = _crossings[0];1177x0 = curxo >> 1;1178if (x0 < minX) {1179minX = x0; // subpixel coordinate1180}11811182x1 = _crossings[numCrossings - 1] >> 1;1183if (x1 > maxX) {1184maxX = x1; // subpixel coordinate1185}118611871188// compute pixel coverages1189prev = curx = x0;1190// to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1.1191// last bit contains orientation (0 or 1)1192crorientation = ((curxo & 0x1) << 1) - 1;11931194if (windingRuleEvenOdd) {1195sum = crorientation;11961197// Even Odd winding rule: take care of mask ie sum(orientations)1198for (i = 1; i < numCrossings; i++) {1199curxo = _crossings[i];1200curx = curxo >> 1;1201// to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1.1202// last bit contains orientation (0 or 1)1203crorientation = ((curxo & 0x1) << 1) - 1;12041205if ((sum & 0x1) != 0) {1206// TODO: perform line clipping on left-right sides1207// to avoid such bound checks:1208x0 = (prev > bboxx0) ? prev : bboxx0;1209x1 = (curx < bboxx1) ? curx : bboxx1;12101211if (x0 < x1) {1212x0 -= bboxx0; // turn x0, x1 from coords to indices1213x1 -= bboxx0; // in the alpha array.12141215pix_x = x0 >> _SUBPIXEL_LG_POSITIONS_X;1216pix_xmaxm1 = (x1 - 1) >> _SUBPIXEL_LG_POSITIONS_X;12171218if (pix_x == pix_xmaxm1) {1219// Start and end in same pixel1220tmp = (x1 - x0); // number of subpixels1221_alpha[pix_x ] += tmp;1222_alpha[pix_x + 1] -= tmp;12231224if (useBlkFlags) {1225// flag used blocks:1226_blkFlags[pix_x >> _BLK_SIZE_LG] = 1;1227}1228} else {1229tmp = (x0 & _SUBPIXEL_MASK_X);1230_alpha[pix_x ]1231+= (_SUBPIXEL_POSITIONS_X - tmp);1232_alpha[pix_x + 1]1233+= tmp;12341235pix_xmax = x1 >> _SUBPIXEL_LG_POSITIONS_X;12361237tmp = (x1 & _SUBPIXEL_MASK_X);1238_alpha[pix_xmax ]1239-= (_SUBPIXEL_POSITIONS_X - tmp);1240_alpha[pix_xmax + 1]1241-= tmp;12421243if (useBlkFlags) {1244// flag used blocks:1245_blkFlags[pix_x >> _BLK_SIZE_LG] = 1;1246_blkFlags[pix_xmax >> _BLK_SIZE_LG] = 1;1247}1248}1249}1250}12511252sum += crorientation;1253prev = curx;1254}1255} else {1256// Non-zero winding rule: optimize that case (default)1257// and avoid processing intermediate crossings1258for (i = 1, sum = 0;; i++) {1259sum += crorientation;12601261if (sum != 0) {1262// prev = min(curx)1263if (prev > curx) {1264prev = curx;1265}1266} else {1267// TODO: perform line clipping on left-right sides1268// to avoid such bound checks:1269x0 = (prev > bboxx0) ? prev : bboxx0;1270x1 = (curx < bboxx1) ? curx : bboxx1;12711272if (x0 < x1) {1273x0 -= bboxx0; // turn x0, x1 from coords to indices1274x1 -= bboxx0; // in the alpha array.12751276pix_x = x0 >> _SUBPIXEL_LG_POSITIONS_X;1277pix_xmaxm1 = (x1 - 1) >> _SUBPIXEL_LG_POSITIONS_X;12781279if (pix_x == pix_xmaxm1) {1280// Start and end in same pixel1281tmp = (x1 - x0); // number of subpixels1282_alpha[pix_x ] += tmp;1283_alpha[pix_x + 1] -= tmp;12841285if (useBlkFlags) {1286// flag used blocks:1287_blkFlags[pix_x >> _BLK_SIZE_LG] = 1;1288}1289} else {1290tmp = (x0 & _SUBPIXEL_MASK_X);1291_alpha[pix_x ]1292+= (_SUBPIXEL_POSITIONS_X - tmp);1293_alpha[pix_x + 1]1294+= tmp;12951296pix_xmax = x1 >> _SUBPIXEL_LG_POSITIONS_X;12971298tmp = (x1 & _SUBPIXEL_MASK_X);1299_alpha[pix_xmax ]1300-= (_SUBPIXEL_POSITIONS_X - tmp);1301_alpha[pix_xmax + 1]1302-= tmp;13031304if (useBlkFlags) {1305// flag used blocks:1306_blkFlags[pix_x >> _BLK_SIZE_LG] = 1;1307_blkFlags[pix_xmax >> _BLK_SIZE_LG] = 1;1308}1309}1310}1311prev = _MAX_VALUE;1312}13131314if (i == numCrossings) {1315break;1316}13171318curxo = _crossings[i];1319curx = curxo >> 1;1320// to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1.1321// last bit contains orientation (0 or 1)1322crorientation = ((curxo & 0x1) << 1) - 1;1323}1324}1325} // numCrossings > 013261327// even if this last row had no crossings, alpha will be zeroed1328// from the last emitRow call. But this doesn't matter because1329// maxX < minX, so no row will be emitted to the MarlinCache.1330if ((y & _SUBPIXEL_MASK_Y) == _SUBPIXEL_MASK_Y) {1331lastY = y >> _SUBPIXEL_LG_POSITIONS_Y;13321333// convert subpixel to pixel coordinate within boundaries:1334minX = FloatMath.max(minX, bboxx0) >> _SUBPIXEL_LG_POSITIONS_X;1335maxX = FloatMath.min(maxX, bboxx1) >> _SUBPIXEL_LG_POSITIONS_X;13361337if (maxX >= minX) {1338// note: alpha array will be zeroed by copyAARow()1339// +2 because alpha [pix_minX; pix_maxX+1]1340// fix range [x0; x1[1341copyAARow(_alpha, lastY, minX, maxX + 2, useBlkFlags);13421343// speculative for next pixel row (scanline coherence):1344if (_enableBlkFlagsHeuristics) {1345// Use block flags if large pixel span and few crossings:1346// ie mean(distance between crossings) is larger than1347// 1 block size;13481349// fast check width:1350maxX -= minX;13511352// if stroking: numCrossings /= 21353// => shift numCrossings by 11354// condition = (width / (numCrossings - 1)) > blockSize1355useBlkFlags = (maxX > _BLK_SIZE) && (maxX >1356(((numCrossings >> stroking) - 1) << _BLK_SIZE_LG));13571358if (doStats) {1359tmp = FloatMath.max(1,1360((numCrossings >> stroking) - 1));1361RendererContext.stats.hist_tile_generator_encoding_dist1362.add(maxX / tmp);1363}1364}1365} else {1366_cache.clearAARow(lastY);1367}1368minX = _MAX_VALUE;1369maxX = _MIN_VALUE;1370}1371} // scan line iterator13721373// Emit final row1374y--;1375y >>= _SUBPIXEL_LG_POSITIONS_Y;13761377// convert subpixel to pixel coordinate within boundaries:1378minX = FloatMath.max(minX, bboxx0) >> _SUBPIXEL_LG_POSITIONS_X;1379maxX = FloatMath.min(maxX, bboxx1) >> _SUBPIXEL_LG_POSITIONS_X;13801381if (maxX >= minX) {1382// note: alpha array will be zeroed by copyAARow()1383// +2 because alpha [pix_minX; pix_maxX+1]1384// fix range [x0; x1[1385copyAARow(_alpha, y, minX, maxX + 2, useBlkFlags);1386} else if (y != lastY) {1387_cache.clearAARow(y);1388}13891390// update member:1391edgeCount = numCrossings;1392prevUseBlkFlags = useBlkFlags;13931394if (doStats) {1395// update max used mark1396activeEdgeMaxUsed = _arrayMaxUsed;1397}1398}13991400boolean endRendering() {1401if (doMonitors) {1402RendererContext.stats.mon_rdr_endRendering.start();1403}1404if (edgeMinY == Integer.MAX_VALUE) {1405return false; // undefined edges bounds1406}14071408final int _boundsMinY = boundsMinY;1409final int _boundsMaxY = boundsMaxY;14101411// bounds as inclusive intervals1412final int spminX = FloatMath.max(FloatMath.ceil_int(edgeMinX - 0.5f), boundsMinX);1413final int spmaxX = FloatMath.min(FloatMath.ceil_int(edgeMaxX - 0.5f), boundsMaxX - 1);14141415// edge Min/Max Y are already rounded to subpixels within bounds:1416final int spminY = edgeMinY;1417final int spmaxY;1418int maxY = edgeMaxY;14191420if (maxY <= _boundsMaxY - 1) {1421spmaxY = maxY;1422} else {1423spmaxY = _boundsMaxY - 1;1424maxY = _boundsMaxY;1425}1426buckets_minY = spminY - _boundsMinY;1427buckets_maxY = maxY - _boundsMinY;14281429if (doLogBounds) {1430MarlinUtils.logInfo("edgesXY = [" + edgeMinX + " ... " + edgeMaxX1431+ "][" + edgeMinY + " ... " + edgeMaxY + "]");1432MarlinUtils.logInfo("spXY = [" + spminX + " ... " + spmaxX1433+ "][" + spminY + " ... " + spmaxY + "]");1434}14351436// test clipping for shapes out of bounds1437if ((spminX > spmaxX) || (spminY > spmaxY)) {1438return false;1439}14401441// half open intervals1442// inclusive:1443final int pminX = spminX >> SUBPIXEL_LG_POSITIONS_X;1444// exclusive:1445final int pmaxX = (spmaxX + SUBPIXEL_MASK_X) >> SUBPIXEL_LG_POSITIONS_X;1446// inclusive:1447final int pminY = spminY >> SUBPIXEL_LG_POSITIONS_Y;1448// exclusive:1449final int pmaxY = (spmaxY + SUBPIXEL_MASK_Y) >> SUBPIXEL_LG_POSITIONS_Y;14501451// store BBox to answer ptg.getBBox():1452this.cache.init(pminX, pminY, pmaxX, pmaxY, edgeSumDeltaY);14531454// Heuristics for using block flags:1455if (ENABLE_BLOCK_FLAGS) {1456enableBlkFlags = this.cache.useRLE;1457prevUseBlkFlags = enableBlkFlags && !ENABLE_BLOCK_FLAGS_HEURISTICS;14581459if (enableBlkFlags) {1460// ensure blockFlags array is large enough:1461// note: +2 to ensure enough space left at end1462final int nxTiles = ((pmaxX - pminX) >> TILE_SIZE_LG) + 2;1463if (nxTiles > INITIAL_ARRAY) {1464blkFlags = rdrCtx.getIntArray(nxTiles);1465}1466}1467}14681469// memorize the rendering bounding box:1470/* note: bbox_spminX and bbox_spmaxX must be pixel boundaries1471to have correct coverage computation */1472// inclusive:1473bbox_spminX = pminX << SUBPIXEL_LG_POSITIONS_X;1474// exclusive:1475bbox_spmaxX = pmaxX << SUBPIXEL_LG_POSITIONS_X;1476// inclusive:1477bbox_spminY = spminY;1478// exclusive:1479bbox_spmaxY = FloatMath.min(spmaxY + 1, pmaxY << SUBPIXEL_LG_POSITIONS_Y);14801481if (doLogBounds) {1482MarlinUtils.logInfo("pXY = [" + pminX + " ... " + pmaxX1483+ "[ [" + pminY + " ... " + pmaxY + "[");1484MarlinUtils.logInfo("bbox_spXY = [" + bbox_spminX + " ... "1485+ bbox_spmaxX + "[ [" + bbox_spminY + " ... "1486+ bbox_spmaxY + "[");1487}14881489// Prepare alpha line:1490// add 2 to better deal with the last pixel in a pixel row.1491final int width = (pmaxX - pminX) + 2;14921493// Useful when processing tile line by tile line1494if (width > INITIAL_AA_ARRAY) {1495if (doStats) {1496RendererContext.stats.stat_array_renderer_alphaline1497.add(width);1498}1499alphaLine = rdrCtx.getIntArray(width);1500}15011502// process first tile line:1503endRendering(pminY);15041505return true;1506}15071508private int bbox_spminX, bbox_spmaxX, bbox_spminY, bbox_spmaxY;15091510void endRendering(final int pminY) {1511if (doMonitors) {1512RendererContext.stats.mon_rdr_endRendering_Y.start();1513}15141515final int spminY = pminY << SUBPIXEL_LG_POSITIONS_Y;1516final int fixed_spminY = FloatMath.max(bbox_spminY, spminY);15171518// avoid rendering for last call to nextTile()1519if (fixed_spminY < bbox_spmaxY) {1520// process a complete tile line ie scanlines for 32 rows1521final int spmaxY = FloatMath.min(bbox_spmaxY, spminY + SUBPIXEL_TILE);15221523// process tile line [0 - 32]1524cache.resetTileLine(pminY);15251526// Process only one tile line:1527_endRendering(fixed_spminY, spmaxY);1528}1529if (doMonitors) {1530RendererContext.stats.mon_rdr_endRendering_Y.stop();1531}1532}15331534private boolean enableBlkFlags = false;1535private boolean prevUseBlkFlags = false;15361537private final int[] blkFlags_initial = new int[INITIAL_ARRAY]; // 1 tile line1538/* block flags (0|1) */1539private int[] blkFlags = blkFlags_initial;15401541void copyAARow(final int[] alphaRow,1542final int pix_y, final int pix_from, final int pix_to,1543final boolean useBlockFlags)1544{1545if (useBlockFlags) {1546if (doStats) {1547RendererContext.stats.hist_tile_generator_encoding.add(1);1548}1549cache.copyAARowRLE_WithBlockFlags(blkFlags, alphaRow, pix_y, pix_from, pix_to);1550} else {1551if (doStats) {1552RendererContext.stats.hist_tile_generator_encoding.add(0);1553}1554cache.copyAARowNoRLE(alphaRow, pix_y, pix_from, pix_to);1555}1556}1557}155815591560