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 com.sun.marlin;
  27 
  28 import java.util.concurrent.atomic.AtomicInteger;
  29 import com.sun.util.reentrant.ReentrantContext;
  30 import com.sun.javafx.geom.Rectangle;
  31 import com.sun.marlin.ArrayCacheConst.CacheStats;
  32 
  33 /**
  34  * This class is a renderer context dedicated to a single thread
  35  */
  36 public final class DRendererContext extends ReentrantContext implements MarlinConst {
  37 
  38     // RendererContext creation counter
  39     private static final AtomicInteger CTX_COUNT = new AtomicInteger(1);
  40 
  41     /**
  42      * Create a new renderer context
  43      *
  44      * @return new RendererContext instance
  45      */
  46     public static DRendererContext createContext() {
  47         return new DRendererContext("ctx"
  48                        + Integer.toString(CTX_COUNT.getAndIncrement()));
  49     }
  50 
  51     // Smallest object used as Cleaner's parent reference
  52     private final Object cleanerObj;
  53     // dirty flag indicating an exception occured during pipeline in pathTo()
  54     public boolean dirty = false;
  55     // shared data
  56     public final float[] float6 = new float[6];
  57     // shared curve (dirty) (Renderer / Stroker)
  58     final DCurve curve = new DCurve();
  59     // MarlinRenderingEngine.TransformingPathConsumer2D
  60     public final DTransformingPathConsumer2D transformerPC2D;
  61     public final DRenderer renderer;
  62     private DRendererNoAA rendererNoAA = null;
  63     public final DStroker stroker;
  64     // Simplifies out collinear lines
  65     public final DCollinearSimplifier simplifier = new DCollinearSimplifier();
  66     public final DDasher dasher;
  67     // flag indicating the shape is stroked (1) or filled (0)
  68     int stroking = 0;
  69 
  70 // MarlinFX specific:
  71     // dirty bbox rectangle
  72     public final Rectangle clip = new Rectangle();
  73     // dirty MaskMarlinAlphaConsumer
  74     public MaskMarlinAlphaConsumer consumer = null;
  75 
  76     // Array caches:
  77     /* clean int[] cache (zero-filled) = 5 refs */
  78     private final IntArrayCache cleanIntCache = new IntArrayCache(true, 5);
  79     /* dirty int[] cache = 4 refs */
  80     private final IntArrayCache dirtyIntCache = new IntArrayCache(false, 4);
  81     /* dirty double[] cache = 3 refs */
  82     private final DoubleArrayCache dirtyDoubleCache = new DoubleArrayCache(false, 3);
  83     /* dirty byte[] cache = 1 ref */
  84     private final ByteArrayCache dirtyByteCache = new ByteArrayCache(false, 1);
  85 
  86     // RendererContext statistics
  87     final RendererStats stats;
  88 
  89     /**
  90      * Constructor
  91      *
  92      * @param name context name (debugging)
  93      */
  94     DRendererContext(final String name) {
  95         if (LOG_CREATE_CONTEXT) {
  96             MarlinUtils.logInfo("new RendererContext = " + name);
  97         }
  98         this.cleanerObj = new Object();
  99 
 100         // create first stats (needed by newOffHeapArray):
 101         if (DO_STATS || DO_MONITORS) {
 102             stats = RendererStats.createInstance(cleanerObj, name);
 103             // push cache stats:
 104             stats.cacheStats = new CacheStats[] { cleanIntCache.stats,
 105                 dirtyIntCache.stats, dirtyDoubleCache.stats, dirtyByteCache.stats
 106             };
 107         } else {
 108             stats = null;
 109         }
 110 
 111         // MarlinRenderingEngine.TransformingPathConsumer2D
 112         transformerPC2D = new DTransformingPathConsumer2D();
 113 
 114         // Renderer:
 115         renderer = new DRenderer(this);
 116 
 117         stroker = new DStroker(this);
 118         dasher = new DDasher(this);
 119     }
 120 
 121     /**
 122      * Disposes this renderer context:
 123      * clean up before reusing this context
 124      */
 125     public void dispose() {
 126         if (DO_STATS) {
 127             if (stats.totalOffHeap > stats.totalOffHeapMax) {
 128                 stats.totalOffHeapMax = stats.totalOffHeap;
 129             }
 130             stats.totalOffHeap = 0L;
 131         }
 132         stroking = 0;
 133         // if context is maked as DIRTY:
 134         if (dirty) {
 135             // may happen if an exception if thrown in the pipeline processing:
 136             // force cleanup of all possible pipelined blocks (except Renderer):
 137 
 138             // Dasher:
 139             this.dasher.dispose();
 140             // Stroker:
 141             this.stroker.dispose();
 142 
 143             // mark context as CLEAN:
 144             dirty = false;
 145         }
 146     }
 147 
 148     public DRendererNoAA getRendererNoAA() {
 149         if (rendererNoAA == null) {
 150             rendererNoAA = new DRendererNoAA(this);
 151         }
 152         return rendererNoAA;
 153     }
 154 
 155     OffHeapArray newOffHeapArray(final long initialSize) {
 156         if (DO_STATS) {
 157             stats.totalOffHeapInitial += initialSize;
 158         }
 159         return new OffHeapArray(cleanerObj, initialSize);
 160     }
 161 
 162     IntArrayCache.Reference newCleanIntArrayRef(final int initialSize) {
 163         return cleanIntCache.createRef(initialSize);
 164     }
 165 
 166     IntArrayCache.Reference newDirtyIntArrayRef(final int initialSize) {
 167         return dirtyIntCache.createRef(initialSize);
 168     }
 169 
 170     DoubleArrayCache.Reference newDirtyDoubleArrayRef(final int initialSize) {
 171         return dirtyDoubleCache.createRef(initialSize);
 172     }
 173 
 174     ByteArrayCache.Reference newDirtyByteArrayRef(final int initialSize) {
 175         return dirtyByteCache.createRef(initialSize);
 176     }
 177 }