1 /* 2 * Copyright (c) 2015, 2017, 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 package com.sun.marlin; 27 28 import com.sun.javafx.geom.Path2D; 29 import java.util.concurrent.atomic.AtomicInteger; 30 import com.sun.util.reentrant.ReentrantContext; 31 import com.sun.javafx.geom.Rectangle; 32 import com.sun.marlin.ArrayCacheConst.CacheStats; 33 import java.lang.ref.WeakReference; 34 35 /** 36 * This class is a renderer context dedicated to a single thread 37 */ 38 public final class RendererContext extends ReentrantContext implements MarlinConst { 39 40 // RendererContext creation counter 41 private static final AtomicInteger CTX_COUNT = new AtomicInteger(1); 42 43 /** 44 * Create a new renderer context 45 * 46 * @return new RendererContext instance 47 */ 48 public static RendererContext createContext() { 49 return new RendererContext("ctx" 50 + Integer.toString(CTX_COUNT.getAndIncrement())); 51 } 52 53 // Smallest object used as Cleaner's parent reference 54 private final Object cleanerObj; 55 // dirty flag indicating an exception occured during pipeline in pathTo() 56 public boolean dirty = false; 57 // shared data 58 public final float[] float6 = new float[6]; 59 // shared curve (dirty) (Renderer / Stroker) 60 final Curve curve = new Curve(); 61 // MarlinRenderingEngine.TransformingPathConsumer2D 62 public final TransformingPathConsumer2D transformerPC2D; 63 // recycled Path2D instance (weak) 64 private WeakReference<Path2D> refPath2D = null; 65 // shared memory between renderer instances: 66 final RendererSharedMemory rdrMem; 67 public final Renderer renderer; 68 private RendererNoAA rendererNoAA = null; 69 public final Stroker stroker; 70 // Simplifies out collinear lines 71 public final CollinearSimplifier simplifier = new CollinearSimplifier(); 72 public final Dasher dasher; 73 // flag indicating the shape is stroked (1) or filled (0) 74 int stroking = 0; 75 // flag indicating to clip the shape 76 public boolean doClip = false; 77 // flag indicating if the path is closed or not (in advance) to handle properly caps 78 boolean closedPath = false; 79 // clip rectangle (ymin, ymax, xmin, xmax): 80 public final float[] clipRect = new float[4]; 81 82 // MarlinFX specific: 83 // dirty bbox rectangle 84 public final Rectangle clip = new Rectangle(); 85 // dirty MaskMarlinAlphaConsumer 86 public MaskMarlinAlphaConsumer consumer = null; 87 88 // Array caches: 89 /* clean int[] cache (zero-filled) = 5 refs */ 90 private final IntArrayCache cleanIntCache = new IntArrayCache(true, 5); 91 /* dirty int[] cache = 5 refs */ 92 private final IntArrayCache dirtyIntCache = new IntArrayCache(false, 5); 93 /* dirty float[] cache = 4 refs (2 polystack) */ 94 private final FloatArrayCache dirtyFloatCache = new FloatArrayCache(false, 4); 95 /* dirty byte[] cache = 2 ref (2 polystack) */ 96 private final ByteArrayCache dirtyByteCache = new ByteArrayCache(false, 2); 97 98 // RendererContext statistics 99 final RendererStats stats; 100 101 /** 102 * Constructor 103 * 104 * @param name context name (debugging) 105 */ 106 RendererContext(final String name) { 107 if (LOG_CREATE_CONTEXT) { 108 MarlinUtils.logInfo("new RendererContext = " + name); 109 } 110 this.cleanerObj = new Object(); 111 112 // create first stats (needed by newOffHeapArray): 113 if (DO_STATS || DO_MONITORS) { 114 stats = RendererStats.createInstance(cleanerObj, name); 115 // push cache stats: 116 stats.cacheStats = new CacheStats[] { cleanIntCache.stats, 117 dirtyIntCache.stats, dirtyFloatCache.stats, dirtyByteCache.stats 118 }; 119 } else { 120 stats = null; 121 } 122 123 // MarlinRenderingEngine.TransformingPathConsumer2D 124 transformerPC2D = new TransformingPathConsumer2D(this); 125 126 // Renderer shared memory: 127 rdrMem = new RendererSharedMemory(this); 128 129 // Renderer: 130 renderer = new Renderer(this); 131 132 stroker = new Stroker(this); 133 dasher = new Dasher(this); 134 } 135 136 /** 137 * Disposes this renderer context: 138 * clean up before reusing this context 139 */ 140 public void dispose() { 141 if (DO_STATS) { 142 if (stats.totalOffHeap > stats.totalOffHeapMax) { 143 stats.totalOffHeapMax = stats.totalOffHeap; 144 } 145 stats.totalOffHeap = 0L; 146 } 147 stroking = 0; 148 doClip = false; 149 closedPath = false; 150 151 // if context is maked as DIRTY: 152 if (dirty) { 153 // may happen if an exception if thrown in the pipeline processing: 154 // force cleanup of all possible pipelined blocks (except Renderer): 155 156 // Dasher: 157 this.dasher.dispose(); 158 // Stroker: 159 this.stroker.dispose(); 160 161 // mark context as CLEAN: 162 dirty = false; 163 } 164 } 165 166 public Path2D getPath2D() { 167 // resolve reference: 168 Path2D p2d = (refPath2D != null) ? refPath2D.get() : null; 169 170 // create a new Path2D ? 171 if (p2d == null) { 172 p2d = new Path2D(WIND_NON_ZERO, INITIAL_EDGES_COUNT); // 32K 173 174 // update weak reference: 175 refPath2D = new WeakReference<Path2D>(p2d); 176 } 177 // reset the path anyway: 178 p2d.reset(); 179 return p2d; 180 } 181 182 public RendererNoAA getRendererNoAA() { 183 if (rendererNoAA == null) { 184 rendererNoAA = new RendererNoAA(this); 185 } 186 return rendererNoAA; 187 } 188 189 OffHeapArray newOffHeapArray(final long initialSize) { 190 if (DO_STATS) { 191 stats.totalOffHeapInitial += initialSize; 192 } 193 return new OffHeapArray(cleanerObj, initialSize); 194 } 195 196 IntArrayCache.Reference newCleanIntArrayRef(final int initialSize) { 197 return cleanIntCache.createRef(initialSize); 198 } 199 200 IntArrayCache.Reference newDirtyIntArrayRef(final int initialSize) { 201 return dirtyIntCache.createRef(initialSize); 202 } 203 204 FloatArrayCache.Reference newDirtyFloatArrayRef(final int initialSize) { 205 return dirtyFloatCache.createRef(initialSize); 206 } 207 208 ByteArrayCache.Reference newDirtyByteArrayRef(final int initialSize) { 209 return dirtyByteCache.createRef(initialSize); 210 } 211 212 static final class RendererSharedMemory { 213 214 // edges [ints] stored in off-heap memory 215 final OffHeapArray edges; 216 217 // edgeBuckets ref (clean) 218 final IntArrayCache.Reference edgeBuckets_ref; 219 // edgeBucketCounts ref (clean) 220 final IntArrayCache.Reference edgeBucketCounts_ref; 221 222 // alphaLine ref (clean) 223 final IntArrayCache.Reference alphaLine_ref; 224 225 // crossings ref (dirty) 226 final IntArrayCache.Reference crossings_ref; 227 // edgePtrs ref (dirty) 228 final IntArrayCache.Reference edgePtrs_ref; 229 // merge sort initial arrays 230 // aux_crossings ref (dirty) 231 final IntArrayCache.Reference aux_crossings_ref; 232 // aux_edgePtrs ref (dirty) 233 final IntArrayCache.Reference aux_edgePtrs_ref; 234 235 // blkFlags ref (clean) 236 final IntArrayCache.Reference blkFlags_ref; 237 238 RendererSharedMemory(final RendererContext rdrCtx) { 239 edges = rdrCtx.newOffHeapArray(INITIAL_EDGES_CAPACITY); // 96K 240 241 edgeBuckets_ref = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K 242 edgeBucketCounts_ref = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K 243 244 // 2048 (pixelsize) pixel large 245 alphaLine_ref = rdrCtx.newCleanIntArrayRef(INITIAL_AA_ARRAY); // 8K 246 247 crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 248 aux_crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 249 edgePtrs_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 250 aux_edgePtrs_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 251 252 blkFlags_ref = rdrCtx.newCleanIntArrayRef(INITIAL_ARRAY); // 1K = 1 tile line 253 } 254 } 255 }