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 76 // MarlinFX specific: 77 // dirty bbox rectangle 78 public final Rectangle clip = new Rectangle(); 79 // dirty MaskMarlinAlphaConsumer 80 public MaskMarlinAlphaConsumer consumer = null; 81 82 // Array caches: 83 /* clean int[] cache (zero-filled) = 4 refs */ 84 private final IntArrayCache cleanIntCache = new IntArrayCache(true, 4); 85 /* dirty int[] cache = 4 refs */ 86 private final IntArrayCache dirtyIntCache = new IntArrayCache(false, 4); 87 /* dirty float[] cache = 3 refs */ 88 private final FloatArrayCache dirtyFloatCache = new FloatArrayCache(false, 3); 89 /* dirty byte[] cache = 1 ref */ 90 private final ByteArrayCache dirtyByteCache = new ByteArrayCache(false, 1); 91 92 // RendererContext statistics 93 final RendererStats stats; 94 95 /** 96 * Constructor 97 * 98 * @param name context name (debugging) 99 */ 100 RendererContext(final String name) { 101 if (LOG_CREATE_CONTEXT) { 102 MarlinUtils.logInfo("new RendererContext = " + name); 103 } 104 this.cleanerObj = new Object(); 105 106 // create first stats (needed by newOffHeapArray): 107 if (DO_STATS || DO_MONITORS) { 108 stats = RendererStats.createInstance(cleanerObj, name); 109 // push cache stats: 110 stats.cacheStats = new CacheStats[] { cleanIntCache.stats, 111 dirtyIntCache.stats, dirtyFloatCache.stats, dirtyByteCache.stats 112 }; 113 } else { 114 stats = null; 115 } 116 117 // MarlinRenderingEngine.TransformingPathConsumer2D 118 transformerPC2D = new TransformingPathConsumer2D(); 119 120 // Renderer shared memory: 121 rdrMem = new RendererSharedMemory(this); 122 123 // Renderer: 124 renderer = new Renderer(this); 125 126 stroker = new Stroker(this); 127 dasher = new Dasher(this); 128 } 129 130 /** 131 * Disposes this renderer context: 132 * clean up before reusing this context 133 */ 134 public void dispose() { 135 if (DO_STATS) { 136 if (stats.totalOffHeap > stats.totalOffHeapMax) { 137 stats.totalOffHeapMax = stats.totalOffHeap; 138 } 139 stats.totalOffHeap = 0L; 140 } 141 stroking = 0; 142 // if context is maked as DIRTY: 143 if (dirty) { 144 // may happen if an exception if thrown in the pipeline processing: 145 // force cleanup of all possible pipelined blocks (except Renderer): 146 147 // Dasher: 148 this.dasher.dispose(); 149 // Stroker: 150 this.stroker.dispose(); 151 152 // mark context as CLEAN: 153 dirty = false; 154 } 155 } 156 157 public Path2D getPath2D() { 158 // resolve reference: 159 Path2D p2d = (refPath2D != null) ? refPath2D.get() : null; 160 161 // create a new Path2D ? 162 if (p2d == null) { 163 p2d = new Path2D(Path2D.WIND_NON_ZERO, INITIAL_EDGES_COUNT); // 32K 164 165 // update weak reference: 166 refPath2D = new WeakReference<Path2D>(p2d); 167 } 168 // reset the path anyway: 169 p2d.reset(); 170 return p2d; 171 } 172 173 public RendererNoAA getRendererNoAA() { 174 if (rendererNoAA == null) { 175 rendererNoAA = new RendererNoAA(this); 176 } 177 return rendererNoAA; 178 } 179 180 OffHeapArray newOffHeapArray(final long initialSize) { 181 if (DO_STATS) { 182 stats.totalOffHeapInitial += initialSize; 183 } 184 return new OffHeapArray(cleanerObj, initialSize); 185 } 186 187 IntArrayCache.Reference newCleanIntArrayRef(final int initialSize) { 188 return cleanIntCache.createRef(initialSize); 189 } 190 191 IntArrayCache.Reference newDirtyIntArrayRef(final int initialSize) { 192 return dirtyIntCache.createRef(initialSize); 193 } 194 195 FloatArrayCache.Reference newDirtyFloatArrayRef(final int initialSize) { 196 return dirtyFloatCache.createRef(initialSize); 197 } 198 199 ByteArrayCache.Reference newDirtyByteArrayRef(final int initialSize) { 200 return dirtyByteCache.createRef(initialSize); 201 } 202 203 static final class RendererSharedMemory { 204 205 // edges [ints] stored in off-heap memory 206 final OffHeapArray edges; 207 208 // edgeBuckets ref (clean) 209 final IntArrayCache.Reference edgeBuckets_ref; 210 // edgeBucketCounts ref (clean) 211 final IntArrayCache.Reference edgeBucketCounts_ref; 212 213 // alphaLine ref (clean) 214 final IntArrayCache.Reference alphaLine_ref; 215 216 // crossings ref (dirty) 217 final IntArrayCache.Reference crossings_ref; 218 // edgePtrs ref (dirty) 219 final IntArrayCache.Reference edgePtrs_ref; 220 // merge sort initial arrays 221 // aux_crossings ref (dirty) 222 final IntArrayCache.Reference aux_crossings_ref; 223 // aux_edgePtrs ref (dirty) 224 final IntArrayCache.Reference aux_edgePtrs_ref; 225 226 // blkFlags ref (clean) 227 final IntArrayCache.Reference blkFlags_ref; 228 229 RendererSharedMemory(final RendererContext rdrCtx) { 230 edges = rdrCtx.newOffHeapArray(INITIAL_EDGES_CAPACITY); // 96K 231 232 edgeBuckets_ref = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K 233 edgeBucketCounts_ref = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K 234 235 // 2048 (pixelsize) pixel large 236 alphaLine_ref = rdrCtx.newCleanIntArrayRef(INITIAL_AA_ARRAY); // 8K 237 238 crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 239 aux_crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 240 edgePtrs_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 241 aux_edgePtrs_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 242 243 blkFlags_ref = rdrCtx.newCleanIntArrayRef(INITIAL_ARRAY); // 1K = 1 tile line 244 } 245 } 246 }