1 /*
   2  * Copyright (c) 2010, 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 com.sun.prism.impl;
  27 
  28 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import java.util.Arrays;
  31 import java.util.Collections;
  32 import java.util.List;
  33 import java.util.Properties;
  34 import java.util.StringTokenizer;
  35 import com.sun.javafx.PlatformUtil;
  36 import com.sun.javafx.util.Utils;
  37 
  38 /**
  39  * Contains the runtime arguments used by Prism.
  40  */
  41 public final class PrismSettings {
  42 
  43     public static final boolean verbose;
  44     public static final boolean debug;
  45     public static final boolean trace;
  46     public static final boolean printAllocs;
  47     public static final boolean isVsyncEnabled;
  48     public static final boolean dirtyOptsEnabled;
  49     public static final boolean occlusionCullingEnabled;
  50     public static final boolean scrollCacheOpt;
  51     public static final boolean threadCheck;
  52     public static final boolean cacheSimpleShapes;
  53     public static final boolean cacheComplexShapes;
  54     public static final boolean useNewImageLoader;
  55     public static final List<String> tryOrder;
  56     public static final int prismStatFrequency;
  57     public static final boolean doNativePisces;
  58     public static final String refType;
  59     public static final boolean forceRepaint;
  60     public static final boolean noFallback;
  61     public static final boolean showDirtyRegions;
  62     public static final boolean showOverdraw;
  63     public static final boolean printRenderGraph;
  64     public static final int minRTTSize;
  65     public static final int dirtyRegionCount;
  66     public static final boolean disableBadDriverWarning;
  67     public static final boolean forceGPU;
  68     public static final int maxTextureSize;
  69     public static final int primTextureSize;
  70     public static final boolean disableRegionCaching;
  71     public static final boolean forcePow2;
  72     public static final boolean noClampToZero;
  73     public static final boolean disableD3D9Ex;
  74     public static final boolean allowHiDPIScaling;
  75     public static final long maxVram;
  76     public static final long targetVram;
  77     public static final boolean poolStats;
  78     public static final boolean poolDebug;
  79     public static final boolean disableEffects;
  80     public static final int glyphCacheWidth;
  81     public static final int glyphCacheHeight;
  82     public static final String perfLog;
  83     public static final boolean perfLogExitFlush;
  84     public static final boolean perfLogFirstPaintFlush;
  85     public static final boolean perfLogFirstPaintExit;
  86     public static final boolean superShader;
  87     public static final boolean forceUploadingPainter;
  88     public static final boolean forceAlphaTestShader;
  89     public static final boolean forceNonAntialiasedShape;
  90 
  91 
  92     private PrismSettings() {
  93     }
  94 
  95     private static void printBooleanOption(boolean opt, String trueStr) {
  96         if (opt) {
  97             System.out.println(trueStr);
  98         } else {
  99             System.out.print("Not ");
 100             System.out.print(Character.toLowerCase(trueStr.charAt(0)));
 101             System.out.println(trueStr.substring(1));
 102         }
 103     }
 104 
 105     static {
 106         final Properties systemProperties =
 107                 (Properties) AccessController.doPrivileged(
 108                         (PrivilegedAction) () -> System.getProperties());
 109 
 110         /* Vsync */
 111         isVsyncEnabled  = getBoolean(systemProperties, "prism.vsync", true)
 112                               && !getBoolean(systemProperties,
 113                                              "javafx.animation.fullspeed",
 114                                              false);
 115 
 116         /* Dirty region optimizations */
 117         dirtyOptsEnabled = getBoolean(systemProperties, "prism.dirtyopts",
 118                                       true);
 119         occlusionCullingEnabled =
 120                 dirtyOptsEnabled && getBoolean(systemProperties,
 121                                                "prism.occlusion.culling",
 122                                                true);
 123 
 124         // The maximum number of dirty regions to use. The absolute max that we can
 125         // support at present is 15.
 126         dirtyRegionCount = Utils.clamp(0, getInt(systemProperties, "prism.dirtyregioncount", 6, null), 15);
 127 
 128         // Scrolling cache optimization
 129         // Disabled as a workaround for RT-39755.
 130         scrollCacheOpt = getBoolean(systemProperties, "prism.scrollcacheopt", false);
 131 
 132         /* Dirty region optimizations */
 133         threadCheck = getBoolean(systemProperties, "prism.threadcheck", false);
 134 
 135         /* Draws overlay rectangles showing where the dirty regions were */
 136         showDirtyRegions = getBoolean(systemProperties, "prism.showdirty", false);
 137 
 138         /*
 139          * Draws overlay rectangles showing not only the dirty regions, but how many times
 140          * each area within that dirty region was drawn (covered by bounds of a drawn object).
 141          */
 142         showOverdraw = getBoolean(systemProperties, "prism.showoverdraw", false);
 143 
 144         /* Prints out the render graph, annotated with dirty opts information */
 145         printRenderGraph = getBoolean(systemProperties, "prism.printrendergraph", false);
 146 
 147         /* Force scene repaint on every frame */
 148         forceRepaint = getBoolean(systemProperties, "prism.forcerepaint", false);
 149 
 150         /* disable fallback to another toolkit if prism couldn't be init-ed */
 151         noFallback = getBoolean(systemProperties, "prism.noFallback", false);
 152 
 153         /* Shape caching optimizations */
 154         String cache = systemProperties.getProperty("prism.cacheshapes",
 155                                                     "complex");
 156         if ("all".equals(cache) || "true".equals(cache)) {
 157             cacheSimpleShapes = true;
 158             cacheComplexShapes = true;
 159         } else if ("complex".equals(cache)) {
 160             cacheSimpleShapes = false;
 161             cacheComplexShapes = true;
 162         } else {
 163             cacheSimpleShapes = false;
 164             cacheComplexShapes = false;
 165         }
 166 
 167         /* New javafx-iio image loader */
 168         useNewImageLoader = getBoolean(systemProperties, "prism.newiio", true);
 169 
 170         /* Verbose output*/
 171         verbose = getBoolean(systemProperties, "prism.verbose", false);
 172 
 173         /* Prism statistics print frequency, <=0 means "do not print" */
 174         prismStatFrequency =
 175                 getInt(systemProperties, "prism.printStats",
 176                        0, 1, "Try -Dprism.printStats=<true or number>");
 177 
 178         /* Debug output*/
 179         debug = getBoolean(systemProperties, "prism.debug", false);
 180 
 181         /* Trace output*/
 182         trace = getBoolean(systemProperties, "prism.trace", false);
 183 
 184         /* Print texture allocation data */
 185         printAllocs = getBoolean(systemProperties, "prism.printallocs", false);
 186 
 187         /* Disable bad driver check warning */
 188         disableBadDriverWarning = getBoolean(systemProperties,
 189                                              "prism.disableBadDriverWarning",
 190                                              false);
 191 
 192         /* Force GPU, if GPU is PS 3 capable, disable GPU qualification check. */
 193         forceGPU = getBoolean(systemProperties, "prism.forceGPU", false);
 194 
 195         String order = systemProperties.getProperty("prism.order");
 196         String[] tryOrderArr;
 197         if (order != null) {
 198             tryOrderArr = split(order, ",");
 199         } else {
 200             if (PlatformUtil.isWindows()) {
 201                 tryOrderArr = new String[] { "d3d", "sw" };
 202             } else if (PlatformUtil.isMac()) {
 203                 tryOrderArr = new String[] { "es2", "sw" };
 204             } else if (PlatformUtil.isIOS()) {
 205                 tryOrderArr = new String[] { "es2" };
 206             } else if (PlatformUtil.isAndroid()) {
 207                     tryOrderArr = new String[] { "es2" };
 208             } else if (PlatformUtil.isLinux()) {
 209                 tryOrderArr = new String[] { "es2", "sw" };
 210             } else {
 211                 tryOrderArr = new String[] { "sw" };
 212             }
 213         }
 214 
 215         tryOrder = Collections.unmodifiableList(Arrays.asList(tryOrderArr));
 216 
 217         String npprop = systemProperties.getProperty("prism.nativepisces");
 218         if (npprop == null) {
 219             doNativePisces = PlatformUtil.isEmbedded() || !PlatformUtil.isLinux();
 220         } else {
 221             doNativePisces = Boolean.parseBoolean(npprop);
 222         }
 223 
 224         String primtex = systemProperties.getProperty("prism.primtextures");
 225         if (primtex == null) {
 226             primTextureSize = PlatformUtil.isEmbedded() ? -1 : 0;
 227         } else if (primtex.equals("true")) {
 228             primTextureSize = -1;
 229         } else if (primtex.equals("false")) {
 230             primTextureSize = 0;
 231         } else {
 232             primTextureSize =
 233                     parseInt(primtex, 0,
 234                              "Try -Dprism.primtextures=[true|false|<number>]");
 235         }
 236 
 237         /* Setting for reference type used by Disposer */
 238         refType = systemProperties.getProperty("prism.reftype");
 239 
 240         forcePow2 = getBoolean(systemProperties, "prism.forcepowerof2", false);
 241         noClampToZero = getBoolean(systemProperties, "prism.noclamptozero", false);
 242 
 243         allowHiDPIScaling = getBoolean(systemProperties, "prism.allowhidpi", true);
 244 
 245         maxVram = getLong(systemProperties, "prism.maxvram", 512 * 1024 * 1024,
 246                           "Try -Dprism.maxvram=<long>[kKmMgG]");
 247         targetVram = getLong(systemProperties, "prism.targetvram", maxVram / 8, maxVram,
 248                              "Try -Dprism.targetvram=<long>[kKmMgG]|<double(0,100)>%");
 249         poolStats = getBoolean(systemProperties, "prism.poolstats", false);
 250         poolDebug = getBoolean(systemProperties, "prism.pooldebug", false);
 251 
 252         if (verbose) {
 253             System.out.print("Prism pipeline init order: ");
 254             for (String s : tryOrder) {
 255                 System.out.print(s+" ");
 256             }
 257             System.out.println("");
 258             String piscestype = (doNativePisces ? "native" : "java");
 259             System.out.println("Using " + piscestype + "-based Pisces rasterizer");
 260             printBooleanOption(dirtyOptsEnabled, "Using dirty region optimizations");
 261             if (primTextureSize == 0) {
 262                 System.out.println("Not using texture mask for primitives");
 263             } else if (primTextureSize < 0) {
 264                 System.out.println("Using system sized mask for primitives");
 265             } else {
 266                 System.out.println("Using "+primTextureSize+" sized mask for primitives");
 267             }
 268             printBooleanOption(forcePow2, "Forcing power of 2 sizes for textures");
 269             printBooleanOption(!noClampToZero, "Using hardware CLAMP_TO_ZERO mode");
 270             printBooleanOption(allowHiDPIScaling, "Opting in for HiDPI pixel scaling");
 271         }
 272 
 273         /*
 274          * Setting for maximum texture size. Default is 4096.
 275          * This will clamp the limit reported by the card to the specified
 276          * value. A value of <= 0 will disable this clamping, causing the
 277          * limit reported by the card to be used without modification.
 278          *
 279          * See RT-21998. This is a workaround for the fact that we don't
 280          * yet handle the case where a texture allocation fails during
 281          * rendering of a very large tiled image.
 282          */
 283         int size = getInt(systemProperties, "prism.maxTextureSize",
 284                           4096, "Try -Dprism.maxTextureSize=<number>");
 285 
 286         if (size <= 0) {
 287             size = Integer.MAX_VALUE;
 288         }
 289         maxTextureSize = size;
 290 
 291         /*
 292          * Check minimum RTT size
 293          * This is needed for some embedded platforms to avoid rendering artifacts
 294          * when rendering into small RTT.
 295          */
 296        minRTTSize = getInt(systemProperties, "prism.minrttsize",
 297                PlatformUtil.isEmbedded() ? 16 : 0, "Try -Dprism.minrttsize=<number>");
 298 
 299         disableRegionCaching = getBoolean(systemProperties,
 300                                           "prism.disableRegionCaching",
 301                                           false);
 302 
 303         disableD3D9Ex = getBoolean(systemProperties, "prism.disableD3D9Ex", false);
 304 
 305         disableEffects = getBoolean(systemProperties, "prism.disableEffects", false);
 306 
 307         glyphCacheWidth = getInt(systemProperties, "prism.glyphCacheWidth", 1024,
 308                 "Try -Dprism.glyphCacheWidth=<number>");
 309         glyphCacheHeight = getInt(systemProperties, "prism.glyphCacheHeight", 1024,
 310                 "Try -Dprism.glyphCacheHeight=<number>");
 311 
 312         /*
 313          * Performance Logger flags
 314          * Enable the performance logger, print on exit, print on first paint etc.
 315          */
 316         perfLog = systemProperties.getProperty("sun.perflog");
 317         perfLogExitFlush = getBoolean(systemProperties, "sun.perflog.fx.exitflush", false, true);
 318         perfLogFirstPaintFlush = getBoolean(systemProperties, "sun.perflog.fx.firstpaintflush", false, true);
 319         perfLogFirstPaintExit = getBoolean(systemProperties, "sun.perflog.fx.firstpaintexit", false, true);
 320 
 321         superShader = getBoolean(systemProperties, "prism.supershader", true);
 322 
 323         // Force uploading painter (e.g., to avoid Linux live-resize jittering)
 324         forceUploadingPainter = getBoolean(systemProperties, "prism.forceUploadingPainter", false);
 325 
 326         // Force the use of fragment shader that does alpha testing (i.e. discard if alpha == 0.0)
 327         forceAlphaTestShader = getBoolean(systemProperties, "prism.forceAlphaTestShader", false);
 328 
 329         // Force non anti-aliasing (not smooth) shape rendering
 330         forceNonAntialiasedShape = getBoolean(systemProperties, "prism.forceNonAntialiasedShape", false);
 331 
 332     }
 333 
 334     private static int parseInt(String s, int dflt, int trueDflt,
 335                                 String errMsg) {
 336         return "true".equalsIgnoreCase(s)
 337                    ? trueDflt
 338                    : parseInt(s, dflt, errMsg);
 339     }
 340 
 341     private static int parseInt(String s, int dflt, String errMsg) {
 342         if (s != null) {
 343             try {
 344                 return Integer.parseInt(s);
 345             } catch (Exception e) {
 346                 if (errMsg != null) {
 347                     System.err.println(errMsg);
 348                 }
 349             }
 350         }
 351 
 352         return dflt;
 353     }
 354 
 355     private static long parseLong(String s, long dflt, long rel, String errMsg) {
 356         if (s != null && s.length() > 0) {
 357             long mult = 1;
 358             if (s.endsWith("%")) {
 359                 if (rel > 0) {
 360                     try {
 361                         s = s.substring(0, s.length() - 1);
 362                         double percent = Double.parseDouble(s);
 363                         if (percent >= 0 && percent <= 100) {
 364                             return Math.round(rel * percent / 100.0);
 365                         }
 366                     } catch (Exception e) {
 367                     }
 368                 }
 369 
 370                 if (errMsg != null) {
 371                     System.err.println(errMsg);
 372                 }
 373                 return dflt;
 374             }
 375             if (s.endsWith("k") || s.endsWith("K")) {
 376                 mult = 1024L;
 377             } else if (s.endsWith("m") || s.endsWith("M")) {
 378                 mult = 1024L * 1024L;
 379             } else if (s.endsWith("g") || s.endsWith("G")) {
 380                 mult = 1024L * 1024L * 1024L;
 381             }
 382             if (mult > 1) {
 383                 s = s.substring(0, s.length() - 1);
 384             }
 385             try {
 386                 return Long.parseLong(s) * mult;
 387             } catch (Exception e) {
 388                 if (errMsg != null) {
 389                     System.err.println(errMsg);
 390                 }
 391             }
 392         }
 393 
 394         return dflt;
 395     }
 396 
 397     /* A simple version of String.split(), since that isn't available on CDC */
 398     private static String[] split(String str, String delim) {
 399         StringTokenizer st = new StringTokenizer(str, delim);
 400         String[] ret = new String[st.countTokens()];
 401         int i = 0;
 402         while (st.hasMoreTokens()) {
 403             ret[i++] = st.nextToken();
 404         }
 405         return ret;
 406     }
 407 
 408     private static boolean getBoolean(Properties properties,
 409                                       String key,
 410                                       boolean dflt) {
 411         final String strval = properties.getProperty(key);
 412         return (strval != null) ? Boolean.parseBoolean(strval) : dflt;
 413     }
 414 
 415     private static boolean getBoolean(Properties properties,
 416                                       String key,
 417                                       boolean dflt,
 418                                       boolean dfltIfDefined) {
 419         final String strval = properties.getProperty(key);
 420         if (strval != null && strval.length() == 0) return dfltIfDefined;
 421         return (strval != null) ? Boolean.parseBoolean(strval) : dflt;
 422     }
 423 
 424     private static int getInt(Properties properties,
 425                               String key,
 426                               int dflt,
 427                               int trueDflt,
 428                               String errMsg) {
 429         return parseInt(properties.getProperty(key),
 430                         dflt,
 431                         trueDflt,
 432                         errMsg);
 433     }
 434 
 435     private static int getInt(Properties properties,
 436                               String key,
 437                               int dflt,
 438                               String errMsg) {
 439         return parseInt(properties.getProperty(key),
 440                         dflt,
 441                         errMsg);
 442     }
 443 
 444     private static long getLong(Properties properties,
 445                                 String key,
 446                                 long dflt,
 447                                 String errMsg)
 448     {
 449         return parseLong(properties.getProperty(key),
 450                          dflt, 0,
 451                          errMsg);
 452     }
 453 
 454     private static long getLong(Properties properties,
 455                                 String key,
 456                                 long dflt, long rel,
 457                                 String errMsg)
 458     {
 459         return parseLong(properties.getProperty(key),
 460                          dflt, rel,
 461                          errMsg);
 462     }
 463 }