1 /* 2 * Copyright (c) 2015, 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 sun.java2d.marlin; 27 28 import java.awt.geom.Path2D; 29 import java.lang.ref.SoftReference; 30 import java.lang.ref.WeakReference; 31 import java.util.concurrent.atomic.AtomicInteger; 32 import static sun.java2d.marlin.ArrayCache.*; 33 import sun.java2d.marlin.MarlinRenderingEngine.NormalizingPathIterator; 34 import static sun.java2d.marlin.MarlinUtils.logInfo; 35 36 /** 37 * This class is a renderer context dedicated to a single thread 38 */ 39 final class RendererContext implements MarlinConst { 40 41 // RendererContext creation counter 42 private static final AtomicInteger contextCount = new AtomicInteger(1); 43 // RendererContext statistics 44 static final RendererStats stats = (doStats || doMonitors) 45 ? RendererStats.getInstance(): null; 46 47 private static final boolean USE_CACHE_HARD_REF = doStats 48 || (MarlinRenderingEngine.REF_TYPE == MarlinRenderingEngine.REF_WEAK); 49 50 /** 51 * Create a new renderer context 52 * 53 * @return new RendererContext instance 54 */ 55 static RendererContext createContext() { 56 final RendererContext newCtx = new RendererContext("ctx" 57 + Integer.toString(contextCount.getAndIncrement())); 58 if (RendererContext.stats != null) { 59 RendererContext.stats.allContexts.add(newCtx); 60 } 61 return newCtx; 62 } 63 64 // context name (debugging purposes) 65 final String name; 66 /* 67 * Reference to this instance (hard, soft or weak). 68 * @see MarlinRenderingEngine#REF_TYPE 69 */ 70 final Object reference; 71 // Smallest object used as Cleaner's parent reference 72 final Object cleanerObj = new Object(); 73 // dirty flag indicating an exception occured during pipeline in pathTo() 74 boolean dirty = false; 75 // dynamic array caches kept using weak reference (low memory footprint) 76 WeakReference<ArrayCachesHolder> refArrayCaches = null; 77 // hard reference to array caches (for statistics) 78 ArrayCachesHolder hardRefArrayCaches = null; 79 // shared data 80 final float[] float6 = new float[6]; 81 // shared curve (dirty) (Renderer / Stroker) 82 final Curve curve = new Curve(); 83 // MarlinRenderingEngine NormalizingPathIterator NearestPixelCenter: 84 final NormalizingPathIterator nPCPathIterator; 85 // MarlinRenderingEngine NearestPixelQuarter NormalizingPathIterator: 86 final NormalizingPathIterator nPQPathIterator; 87 // MarlinRenderingEngine.TransformingPathConsumer2D 88 final TransformingPathConsumer2D transformerPC2D; 89 // recycled Path2D instance 90 Path2D.Float p2d = null; 91 final Renderer renderer; 92 final Stroker stroker; 93 // Simplifies out collinear lines 94 final CollinearSimplifier simplifier = new CollinearSimplifier(); 95 final Dasher dasher; 96 final MarlinTileGenerator ptg; 97 final MarlinCache cache; 98 // flag indicating the shape is stroked (1) or filled (0) 99 int stroking = 0; 100 101 /** 102 * Constructor 103 * 104 * @param name 105 */ 106 RendererContext(final String name) { 107 if (logCreateContext) { 108 MarlinUtils.logInfo("new RendererContext = " + name); 109 } 110 111 this.name = name; 112 113 // NormalizingPathIterator instances: 114 nPCPathIterator = new NormalizingPathIterator.NearestPixelCenter(float6); 115 nPQPathIterator = new NormalizingPathIterator.NearestPixelQuarter(float6); 116 117 // MarlinRenderingEngine.TransformingPathConsumer2D 118 transformerPC2D = new TransformingPathConsumer2D(); 119 120 // Renderer: 121 cache = new MarlinCache(this); 122 renderer = new Renderer(this); // needs MarlinCache from rdrCtx.cache 123 ptg = new MarlinTileGenerator(renderer); 124 125 stroker = new Stroker(this); 126 dasher = new Dasher(this); 127 128 // Create the reference to this instance (hard, soft or weak): 129 switch (MarlinRenderingEngine.REF_TYPE) { 130 default: 131 case MarlinRenderingEngine.REF_HARD: 132 reference = this; 133 break; 134 case MarlinRenderingEngine.REF_SOFT: 135 reference = new SoftReference<RendererContext>(this); 136 break; 137 case MarlinRenderingEngine.REF_WEAK: 138 reference = new WeakReference<RendererContext>(this); 139 break; 140 } 141 } 142 143 /** 144 * Disposes this renderer context: 145 * clean up before reusing this context 146 */ 147 void dispose() { 148 stroking = 0; 149 // reset hard reference to array caches if needed: 150 if (!USE_CACHE_HARD_REF) { 151 hardRefArrayCaches = null; 152 } 153 // if context is maked as DIRTY: 154 if (dirty) { 155 // may happen if an exception if thrown in the pipeline processing: 156 // force cleanup of all possible pipelined blocks (except Renderer): 157 158 // NormalizingPathIterator instances: 159 this.nPCPathIterator.dispose(); 160 this.nPQPathIterator.dispose(); | 1 /* 2 * Copyright (c) 2015, 2016, 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 sun.java2d.marlin; 27 28 import java.awt.geom.Path2D; 29 import java.lang.ref.WeakReference; 30 import java.util.concurrent.atomic.AtomicInteger; 31 import sun.java2d.ReentrantContext; 32 import sun.java2d.ReentrantContextProvider; 33 import static sun.java2d.marlin.ArrayCache.*; 34 import sun.java2d.marlin.MarlinRenderingEngine.NormalizingPathIterator; 35 import static sun.java2d.marlin.MarlinUtils.logInfo; 36 37 /** 38 * This class is a renderer context dedicated to a single thread 39 */ 40 final class RendererContext extends ReentrantContext implements MarlinConst { 41 42 // RendererContext creation counter 43 private static final AtomicInteger contextCount = new AtomicInteger(1); 44 // RendererContext statistics 45 static final RendererStats stats = (doStats || doMonitors) 46 ? RendererStats.getInstance(): null; 47 48 private static final boolean USE_CACHE_HARD_REF = doStats 49 || (MarlinRenderingEngine.REF_TYPE == ReentrantContextProvider.REF_WEAK); 50 51 /** 52 * Create a new renderer context 53 * 54 * @return new RendererContext instance 55 */ 56 static RendererContext createContext() { 57 final RendererContext newCtx = new RendererContext("ctx" 58 + Integer.toString(contextCount.getAndIncrement())); 59 60 if (RendererContext.stats != null) { 61 RendererContext.stats.allContexts.add(newCtx); 62 } 63 return newCtx; 64 } 65 66 // context name (debugging purposes) 67 final String name; 68 // Smallest object used as Cleaner's parent reference 69 final Object cleanerObj = new Object(); 70 // dirty flag indicating an exception occured during pipeline in pathTo() 71 boolean dirty = false; 72 // dynamic array caches kept using weak reference (low memory footprint) 73 WeakReference<ArrayCachesHolder> refArrayCaches = null; 74 // hard reference to array caches (for statistics) 75 ArrayCachesHolder hardRefArrayCaches = null; 76 // shared data 77 final float[] float6 = new float[6]; 78 // shared curve (dirty) (Renderer / Stroker) 79 final Curve curve = new Curve(); 80 // MarlinRenderingEngine NormalizingPathIterator NearestPixelCenter: 81 final NormalizingPathIterator nPCPathIterator; 82 // MarlinRenderingEngine NearestPixelQuarter NormalizingPathIterator: 83 final NormalizingPathIterator nPQPathIterator; 84 // MarlinRenderingEngine.TransformingPathConsumer2D 85 final TransformingPathConsumer2D transformerPC2D; 86 // recycled Path2D instance 87 Path2D.Float p2d = null; 88 final Renderer renderer; 89 final Stroker stroker; 90 // Simplifies out collinear lines 91 final CollinearSimplifier simplifier = new CollinearSimplifier(); 92 final Dasher dasher; 93 final MarlinTileGenerator ptg; 94 final MarlinCache cache; 95 // flag indicating the shape is stroked (1) or filled (0) 96 int stroking = 0; 97 98 /** 99 * Constructor 100 * 101 * @param name context name (debugging) 102 */ 103 RendererContext(final String name) { 104 if (logCreateContext) { 105 MarlinUtils.logInfo("new RendererContext = " + name); 106 } 107 108 this.name = name; 109 110 // NormalizingPathIterator instances: 111 nPCPathIterator = new NormalizingPathIterator.NearestPixelCenter(float6); 112 nPQPathIterator = new NormalizingPathIterator.NearestPixelQuarter(float6); 113 114 // MarlinRenderingEngine.TransformingPathConsumer2D 115 transformerPC2D = new TransformingPathConsumer2D(); 116 117 // Renderer: 118 cache = new MarlinCache(this); 119 renderer = new Renderer(this); // needs MarlinCache from rdrCtx.cache 120 ptg = new MarlinTileGenerator(renderer); 121 122 stroker = new Stroker(this); 123 dasher = new Dasher(this); 124 } 125 126 /** 127 * Disposes this renderer context: 128 * clean up before reusing this context 129 */ 130 void dispose() { 131 stroking = 0; 132 // reset hard reference to array caches if needed: 133 if (!USE_CACHE_HARD_REF) { 134 hardRefArrayCaches = null; 135 } 136 // if context is maked as DIRTY: 137 if (dirty) { 138 // may happen if an exception if thrown in the pipeline processing: 139 // force cleanup of all possible pipelined blocks (except Renderer): 140 141 // NormalizingPathIterator instances: 142 this.nPCPathIterator.dispose(); 143 this.nPQPathIterator.dispose(); |