1 /*
   2  * Copyright (c) 2015, 2018, 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 width used to define initial arrays
  58      * (tile AA chunk, alpha line)
  59      *
  60      * @return 64 < initial pixel size < 32768 (4096 by default)
  61      */
  62     public static int getInitialPixelWidth() {
  63         return align(
  64             getInteger("prism.marlin.pixelWidth", 4096, 64, 32 * 1024),
  65             64);
  66     }
  67 
  68     /**
  69      * Return the initial pixel height used to define initial arrays
  70      * (buckets)
  71      *
  72      * @return 64 < initial pixel size < 32768 (2176 by default)
  73      */
  74     public static int getInitialPixelHeight() {
  75         return align(
  76             getInteger("prism.marlin.pixelHeight", 2176, 64, 32 * 1024),
  77             64);
  78     }
  79 
  80     /**
  81      * Return true if the profile is 'quality' (default) over 'speed'
  82      *
  83      * @return true if the profile is 'quality' (default), false otherwise
  84      */
  85     public static boolean isProfileQuality() {
  86         final String key = "prism.marlin.profile";
  87         final String profile = getString(key, "quality");
  88         if ("quality".equals(profile)) {
  89             return true;
  90         }
  91         if ("speed".equals(profile)) {
  92             return false;
  93         }
  94         logInfo("Invalid value for " + key + " = " + profile
  95                     + "; expect value in [quality, speed] !");
  96         return true;
  97     }
  98 
  99     /**
 100      * Return the log(2) corresponding to subpixel on x-axis
 101      *
 102      * @return 0 (1 subpixels) < initial pixel size < 8 (256 subpixels)
 103      * (8 by default ie 256 subpixels)
 104      */
 105     public static int getSubPixel_Log2_X() {
 106         return getInteger("prism.marlin.subPixel_log2_X", 8, 0, 8);
 107     }
 108 
 109     /**
 110      * Return the log(2) corresponding to subpixel on y-axis
 111      *
 112      * @return 0 (1 subpixels) < initial pixel size < 8 (256 subpixels)
 113      * (3 by default ie 8 subpixels for the quality profile)
 114      * (2 by default ie 4 subpixels for the speed profile)
 115      */
 116     public static int getSubPixel_Log2_Y() {
 117         final int def = isProfileQuality() ? 3 : 2;
 118         return getInteger("prism.marlin.subPixel_log2_Y", def, 0, 8);
 119     }
 120 
 121     /**
 122      * Return the log(2) corresponding to the block size in pixels
 123      *
 124      * @return 3 (8 pixels) < block size < 8 (256 pixels)
 125      * (5 by default ie 32 pixels)
 126      */
 127     public static int getBlockSize_Log2() {
 128         return getInteger("prism.marlin.blockSize_log2", 5, 3, 8);
 129     }
 130 
 131     // RLE / blockFlags settings
 132 
 133     public static boolean isForceRLE() {
 134         return getBoolean("prism.marlin.forceRLE", "false");
 135     }
 136 
 137     public static boolean isForceNoRLE() {
 138         return getBoolean("prism.marlin.forceNoRLE", "false");
 139     }
 140 
 141     public static boolean isUseTileFlags() {
 142         return getBoolean("prism.marlin.useTileFlags", "true");
 143     }
 144 
 145     public static boolean isUseTileFlagsWithHeuristics() {
 146         return isUseTileFlags()
 147         && getBoolean("prism.marlin.useTileFlags.useHeuristics", "true");
 148     }
 149 
 150     public static int getRLEMinWidth() {
 151         return getInteger("prism.marlin.rleMinWidth", 64, 0, Integer.MAX_VALUE);
 152     }
 153 
 154     // optimisation parameters
 155 
 156     public static boolean isUseSimplifier() {
 157         return getBoolean("prism.marlin.useSimplifier", "false");
 158     }
 159 
 160     public static boolean isUsePathSimplifier() {
 161         return getBoolean("prism.marlin.usePathSimplifier", "false");
 162     }
 163 
 164     public static float getPathSimplifierPixelTolerance() {
 165         // default: MIN_PEN_SIZE or less ?
 166         return getFloat("prism.marlin.pathSimplifier.pixTol",
 167                 (1.0f / MarlinConst.MIN_SUBPIXELS),
 168                 1e-3f,
 169                 10.0f);
 170     }
 171 
 172     public static boolean isDoClip() {
 173         return getBoolean("prism.marlin.clip", "true");
 174     }
 175 
 176     public static boolean isDoClipRuntimeFlag() {
 177         return getBoolean("prism.marlin.clip.runtime.enable", "false");
 178     }
 179 
 180     public static boolean isDoClipAtRuntime() {
 181         return getBoolean("prism.marlin.clip.runtime", "true");
 182     }
 183 
 184     public static boolean isDoClipSubdivider() {
 185         return getBoolean("prism.marlin.clip.subdivider", "true");
 186     }
 187 
 188     public static float getSubdividerMinLength() {
 189         return getFloat("prism.marlin.clip.subdivider.minLength", 100.0f, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY);
 190     }
 191 
 192     // debugging parameters
 193 
 194     public static boolean isDoStats() {
 195         return getBoolean("prism.marlin.doStats", "false");
 196     }
 197 
 198     public static boolean isDoMonitors() {
 199         return getBoolean("prism.marlin.doMonitors", "false");
 200     }
 201 
 202     public static boolean isDoChecks() {
 203         return getBoolean("prism.marlin.doChecks", "false");
 204     }
 205 
 206     // logging parameters
 207 
 208     public static boolean isLoggingEnabled() {
 209         return getBoolean("prism.marlin.log", "false");
 210     }
 211 
 212     public static boolean isUseLogger() {
 213         return getBoolean("prism.marlin.useLogger", "false");
 214     }
 215 
 216     public static boolean isLogCreateContext() {
 217         return getBoolean("prism.marlin.logCreateContext", "false");
 218     }
 219 
 220     public static boolean isLogUnsafeMalloc() {
 221         return getBoolean("prism.marlin.logUnsafeMalloc", "false");
 222     }
 223 
 224     // quality settings
 225     public static float getCurveLengthError() {
 226         return getFloat("prism.marlin.curve_len_err", 0.01f, 1e-6f, 1.0f);
 227     }
 228 
 229     public static float getCubicDecD2() {
 230         final float def = isProfileQuality() ? 1.0f : 2.5f;
 231         return getFloat("prism.marlin.cubic_dec_d2", def, 1e-5f, 4.0f);
 232     }
 233 
 234     public static float getCubicIncD1() {
 235         final float def = isProfileQuality() ? 0.2f : 0.5f;
 236         return getFloat("prism.marlin.cubic_inc_d1", def, 1e-6f, 1.0f);
 237     }
 238 
 239     public static float getQuadDecD2() {
 240         final float def = isProfileQuality() ? 0.5f : 1.0f;
 241         return getFloat("prism.marlin.quad_dec_d2", def, 1e-5f, 4.0f);
 242     }
 243 
 244     // system property utilities
 245     static String getString(final String key, final String def) {
 246         return AccessController.doPrivileged(
 247             (PrivilegedAction<String>) () -> {
 248                 String value = System.getProperty(key);
 249                 return (value == null) ? def : value;
 250             });
 251     }
 252 
 253     static boolean getBoolean(final String key, final String def) {
 254         return Boolean.valueOf(AccessController.doPrivileged(
 255             (PrivilegedAction<String>) () -> {
 256                 String value = System.getProperty(key);
 257                 return (value == null) ? def : value;
 258             }));
 259     }
 260 
 261     static int getInteger(final String key, final int def,
 262                                  final int min, final int max)
 263     {
 264         final String property = AccessController.doPrivileged(
 265                     (PrivilegedAction<String>) () -> System.getProperty(key));
 266 
 267         int value = def;
 268         if (property != null) {
 269             try {
 270                 value = Integer.decode(property);
 271             } catch (NumberFormatException e) {
 272                 logInfo("Invalid integer value for " + key + " = " + property);
 273             }
 274         }
 275 
 276         // check for invalid values
 277         if ((value < min) || (value > max)) {
 278             logInfo("Invalid value for " + key + " = " + value
 279                     + "; expected value in range[" + min + ", " + max + "] !");
 280             value = def;
 281         }
 282         return value;
 283     }
 284 
 285     static int align(final int val, final int norm) {
 286         final int ceil = FloatMath.ceil_int( ((float) val) / norm);
 287         return ceil * norm;
 288     }
 289 
 290     public static double getDouble(final String key, final double def,
 291                                    final double min, final double max)
 292     {
 293         double value = def;
 294         final String property = AccessController.doPrivileged(
 295                     (PrivilegedAction<String>) () -> System.getProperty(key));
 296 
 297         if (property != null) {
 298             try {
 299                 value = Double.parseDouble(property);
 300             } catch (NumberFormatException nfe) {
 301                 logInfo("Invalid value for " + key + " = " + property + " !");
 302             }
 303         }
 304         // check for invalid values
 305         if (value < min || value > max) {
 306             logInfo("Invalid value for " + key + " = " + value
 307                     + "; expect value in range[" + min + ", " + max + "] !");
 308             value = def;
 309         }
 310         return value;
 311     }
 312 
 313     public static float getFloat(final String key, final float def,
 314                                  final float min, final float max)
 315     {
 316         return (float)getDouble(key, def, min, max);
 317     }
 318 }