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 java.security.AccessController;
  29 import static com.sun.marlin.MarlinUtils.logInfo;
  30 import java.security.PrivilegedAction;
  31 
  32 public final class MarlinProperties {
  33 
  34     private MarlinProperties() {
  35         // no-op
  36     }
  37 
  38     // marlin system properties
  39 
  40     public static boolean isUseThreadLocal() {
  41         return getBoolean("prism.marlin.useThreadLocal", "true");
  42     }
  43 
  44     /**
  45      * Return the initial edge capacity used to define initial arrays
  46      * (edges, polystack, crossings)
  47      *
  48      * @return 256 < initial edges < 65536 (4096 by default)
  49      */
  50     public static int getInitialEdges() {
  51         return align(
  52             getInteger("prism.marlin.edges", 4096, 64, 64 * 1024),
  53             64);
  54     }
  55 
  56     /**
  57      * Return the initial pixel size used to define initial arrays
  58      * (tile AA chunk, alpha line, buckets)
  59      *
  60      * @return 64 < initial pixel size < 32768 (2048 by default)
  61      */
  62     public static int getInitialImageSize() {
  63         return align(
  64             getInteger("prism.marlin.pixelsize", 2048, 64, 32 * 1024),
  65             64);
  66     }
  67 
  68     /**
  69      * Return the log(2) corresponding to subpixel on x-axis (
  70      *
  71      * @return 0 (1 subpixels) < initial pixel size < 8 (256 subpixels)
  72      * (3 by default ie 8 subpixels)
  73      */
  74     public static int getSubPixel_Log2_X() {
  75         return getInteger("prism.marlin.subPixel_log2_X", 3, 0, 8);
  76     }
  77 
  78     /**
  79      * Return the log(2) corresponding to subpixel on y-axis (
  80      *
  81      * @return 0 (1 subpixels) < initial pixel size < 8 (256 subpixels)
  82      * (3 by default ie 8 subpixels)
  83      */
  84     public static int getSubPixel_Log2_Y() {
  85         return getInteger("prism.marlin.subPixel_log2_Y", 3, 0, 8);
  86     }
  87 
  88     /**
  89      * Return the log(2) corresponding to the block size in pixels
  90      *
  91      * @return 3 (8 pixels) < block size < 8 (256 pixels)
  92      * (5 by default ie 32 pixels)
  93      */
  94     public static int getBlockSize_Log2() {
  95         return getInteger("prism.marlin.blockSize_log2", 5, 3, 8);
  96     }
  97 
  98     // RLE / blockFlags settings
  99 
 100     public static boolean isForceRLE() {
 101         return getBoolean("prism.marlin.forceRLE", "false");
 102     }
 103 
 104     public static boolean isForceNoRLE() {
 105         return getBoolean("prism.marlin.forceNoRLE", "false");
 106     }
 107 
 108     public static boolean isUseTileFlags() {
 109         return getBoolean("prism.marlin.useTileFlags", "true");
 110     }
 111 
 112     public static boolean isUseTileFlagsWithHeuristics() {
 113         return isUseTileFlags()
 114         && getBoolean("prism.marlin.useTileFlags.useHeuristics", "true");
 115     }
 116 
 117     public static int getRLEMinWidth() {
 118         return getInteger("prism.marlin.rleMinWidth", 64, 0, Integer.MAX_VALUE);
 119     }
 120 
 121     // optimisation parameters
 122 
 123     public static boolean isUseSimplifier() {
 124         return getBoolean("prism.marlin.useSimplifier", "false");
 125     }
 126 
 127     // debugging parameters
 128 
 129     public static boolean isDoStats() {
 130         return getBoolean("prism.marlin.doStats", "false");
 131     }
 132 
 133     public static boolean isDoMonitors() {
 134         return getBoolean("prism.marlin.doMonitors", "false");
 135     }
 136 
 137     public static boolean isDoChecks() {
 138         return getBoolean("prism.marlin.doChecks", "false");
 139     }
 140 
 141     // logging parameters
 142 
 143     public static boolean isLoggingEnabled() {
 144         return getBoolean("prism.marlin.log", "false");
 145     }
 146 
 147     public static boolean isUseLogger() {
 148         return getBoolean("prism.marlin.useLogger", "false");
 149     }
 150 
 151     public static boolean isLogCreateContext() {
 152         return getBoolean("prism.marlin.logCreateContext", "false");
 153     }
 154 
 155     public static boolean isLogUnsafeMalloc() {
 156         return getBoolean("prism.marlin.logUnsafeMalloc", "false");
 157     }
 158 
 159     // quality settings
 160 
 161     public static float getCubicDecD2() {
 162         return getFloat("prism.marlin.cubic_dec_d2", 1.0f, 0.01f, 4.0f);
 163     }
 164 
 165     public static float getCubicIncD1() {
 166         return getFloat("prism.marlin.cubic_inc_d1", 0.4f, 0.01f, 2.0f);
 167     }
 168 
 169     public static float getQuadDecD2() {
 170         return getFloat("prism.marlin.quad_dec_d2", 0.5f, 0.01f, 4.0f);
 171     }
 172 
 173     // system property utilities
 174     static boolean getBoolean(final String key, final String def) {
 175         return Boolean.valueOf(AccessController.doPrivileged(
 176             (PrivilegedAction<String>) () -> {
 177                 String value = System.getProperty(key);
 178                 return (value == null) ? def : value;
 179             }));
 180     }
 181 
 182     static int getInteger(final String key, final int def,
 183                                  final int min, final int max)
 184     {
 185         final String property = AccessController.doPrivileged(
 186                     (PrivilegedAction<String>) () -> System.getProperty(key));
 187 
 188         int value = def;
 189         if (property != null) {
 190             try {
 191                 value = Integer.decode(property);
 192             } catch (NumberFormatException e) {
 193                 logInfo("Invalid integer value for " + key + " = " + property);
 194             }
 195         }
 196 
 197         // check for invalid values
 198         if ((value < min) || (value > max)) {
 199             logInfo("Invalid value for " + key + " = " + value
 200                     + "; expected value in range[" + min + ", " + max + "] !");
 201             value = def;
 202         }
 203         return value;
 204     }
 205 
 206     static int align(final int val, final int norm) {
 207         final int ceil = FloatMath.ceil_int( ((float) val) / norm);
 208         return ceil * norm;
 209     }
 210 
 211     public static double getDouble(final String key, final double def,
 212                                    final double min, final double max)
 213     {
 214         double value = def;
 215         final String property = AccessController.doPrivileged(
 216                     (PrivilegedAction<String>) () -> System.getProperty(key));
 217 
 218         if (property != null) {
 219             try {
 220                 value = Double.parseDouble(property);
 221             } catch (NumberFormatException nfe) {
 222                 logInfo("Invalid value for " + key + " = " + property + " !");
 223             }
 224         }
 225         // check for invalid values
 226         if (value < min || value > max) {
 227             logInfo("Invalid value for " + key + " = " + value
 228                     + "; expect value in range[" + min + ", " + max + "] !");
 229             value = def;
 230         }
 231         return value;
 232     }
 233 
 234     public static float getFloat(final String key, final float def,
 235                                  final float min, final float max)
 236     {
 237         return (float)getDouble(key, def, min, max);
 238     }
 239 }