< prev index next >

src/java.desktop/share/classes/sun/java2d/loops/GraphicsPrimitive.java

Print this page




  23  * questions.
  24  */
  25 
  26 /*
  27  * @author Charlton Innovations, Inc.
  28  */
  29 
  30 package sun.java2d.loops;
  31 
  32 import java.awt.image.BufferedImage;
  33 import java.awt.AlphaComposite;
  34 import java.awt.Rectangle;
  35 import sun.awt.image.BufImgSurfaceData;
  36 import sun.awt.util.ThreadGroupUtils;
  37 import sun.java2d.SurfaceData;
  38 import sun.java2d.pipe.Region;
  39 import java.lang.reflect.Field;
  40 import java.util.StringTokenizer;
  41 import java.util.Iterator;
  42 import java.util.HashMap;

  43 import java.util.Map;
  44 import java.io.PrintStream;
  45 import java.io.OutputStream;
  46 import java.io.FileOutputStream;
  47 import java.io.FileNotFoundException;
  48 import java.security.AccessController;
  49 import java.security.PrivilegedAction;
  50 
  51 import sun.security.action.GetPropertyAction;
  52 
  53 /**
  54  * defines interface for primitives which can be placed into
  55  * the graphic component manager framework
  56  */
  57 public abstract class GraphicsPrimitive {
  58 
  59     protected static interface GeneralBinaryOp {
  60         /**
  61          * This method allows the setupGeneralBinaryOp method to set
  62          * the converters into the General version of the Primitive.


 305     //
 306     // A version of satisfies used for regression testing
 307     //
 308     final boolean satisfiesSameAs(GraphicsPrimitive other) {
 309         return (methodSignature == other.methodSignature &&
 310                 sourceType.equals(other.sourceType) &&
 311                 compositeType.equals(other.compositeType) &&
 312                 destType.equals(other.destType));
 313     }
 314 
 315     public abstract GraphicsPrimitive makePrimitive(SurfaceType srctype,
 316                                                     CompositeType comptype,
 317                                                     SurfaceType dsttype);
 318 
 319     public abstract GraphicsPrimitive traceWrap();
 320 
 321     static HashMap<Object, int[]> traceMap;
 322 
 323     public static int traceflags;
 324     public static String tracefile;

 325     public static PrintStream traceout;


 326 
 327     public static final int TRACELOG = 1;
 328     public static final int TRACETIMESTAMP = 2;
 329     public static final int TRACECOUNTS = 4;









 330 
 331     static {
 332         GetPropertyAction gpa = new GetPropertyAction("sun.java2d.trace");
 333         String trace = AccessController.doPrivileged(gpa);
 334         if (trace != null) {
 335             boolean verbose = false;
 336             int traceflags = 0;
 337             StringTokenizer st = new StringTokenizer(trace, ",");
 338             while (st.hasMoreTokens()) {
 339                 String tok = st.nextToken();
 340                 if (tok.equalsIgnoreCase("count")) {
 341                     traceflags |= GraphicsPrimitive.TRACECOUNTS;
 342                 } else if (tok.equalsIgnoreCase("log")) {
 343                     traceflags |= GraphicsPrimitive.TRACELOG;
 344                 } else if (tok.equalsIgnoreCase("timestamp")) {
 345                     traceflags |= GraphicsPrimitive.TRACETIMESTAMP;







 346                 } else if (tok.equalsIgnoreCase("verbose")) {
 347                     verbose = true;
 348                 } else if (tok.regionMatches(true, 0, "out:", 0, 4)) {
 349                     tracefile = tok.substring(4);






 350                 } else {
 351                     if (!tok.equalsIgnoreCase("help")) {
 352                         System.err.println("unrecognized token: "+tok);
 353                     }
 354                     System.err.println("usage: -Dsun.java2d.trace="+
 355                                        "[log[,timestamp]],[count],"+
 356                                        "[out:<filename>],[help],[verbose]");
 357                 }
 358             }



 359             if (verbose) {
 360                 System.err.print("GraphicsPrimitive logging ");
 361                 if ((traceflags & GraphicsPrimitive.TRACELOG) != 0) {
 362                     System.err.println("enabled");
 363                     System.err.print("GraphicsPrimitive timetamps ");
 364                     if ((traceflags & GraphicsPrimitive.TRACETIMESTAMP) != 0) {
 365                         System.err.println("enabled");
 366                     } else {
 367                         System.err.println("disabled");
 368                     }
 369                 } else {
 370                     System.err.println("[and timestamps] disabled");
 371                 }
 372                 System.err.print("GraphicsPrimitive invocation counts ");
 373                 if ((traceflags & GraphicsPrimitive.TRACECOUNTS) != 0) {
 374                     System.err.println("enabled");
 375                 } else {
 376                     System.err.println("disabled");
 377                 }
 378                 System.err.print("GraphicsPrimitive trace output to ");


 399                             try {
 400                                 return new FileOutputStream(tracefile);
 401                             } catch (FileNotFoundException e) {
 402                                 return null;
 403                             }
 404                         }
 405                     });
 406                 if (o != null) {
 407                     traceout = new PrintStream(o);
 408                 } else {
 409                     traceout = System.err;
 410                 }
 411             } else {
 412                 traceout = System.err;
 413             }
 414         }
 415         return traceout;
 416     }
 417 
 418     public static class TraceReporter implements Runnable {
 419         public static void setShutdownHook() {





 420             AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 421                 TraceReporter t = new TraceReporter();
 422                 Thread thread = new Thread(
 423                         ThreadGroupUtils.getRootThreadGroup(), t,
 424                         "TraceReporter", 0, false);
 425                 thread.setContextClassLoader(null);
 426                 Runtime.getRuntime().addShutdownHook(thread);
 427                 return null;
 428             });
 429         }
 430 
 431         public void run() {
 432             PrintStream ps = getTraceOutputFile();

 433             Iterator<Map.Entry<Object, int[]>> iterator =
 434                 traceMap.entrySet().iterator();
 435             long total = 0;
 436             int numprims = 0;
 437             while (iterator.hasNext()) {
 438                 Map.Entry<Object, int[]> me = iterator.next();
 439                 Object prim = me.getKey();
 440                 int[] count = me.getValue();
 441                 if (count[0] == 1) {
 442                     ps.print("1 call to ");
 443                 } else {
 444                     ps.print(count[0]+" calls to ");
 445                 }
 446                 ps.println(prim);
 447                 numprims++;
 448                 total += count[0];
 449             }
 450             if (numprims == 0) {
 451                 ps.println("No graphics primitives executed");
 452             } else if (numprims > 1) {
 453                 ps.println(total+" total calls to "+
 454                            numprims+" different primitives");


 455             }
 456         }




 457     }
 458 
 459     public static synchronized void tracePrimitive(Object prim) {
 460         if ((traceflags & TRACECOUNTS) != 0) {
 461             if (traceMap == null) {
 462                 traceMap = new HashMap<>();
 463                 TraceReporter.setShutdownHook();
 464             }
 465             int[] o = traceMap.get(prim);
 466             if (o == null) {
 467                 o = new int[1];
 468                 traceMap.put(prim, o);
 469             }
 470             o[0]++;
 471         }
 472         if ((traceflags & TRACELOG) != 0) {
 473             PrintStream ps = getTraceOutputFile();
 474             if ((traceflags & TRACETIMESTAMP) != 0) {
 475                 ps.print(System.currentTimeMillis()+": ");
 476             }
 477             ps.println(prim);
 478         }
 479     }
 480 


































 481     protected void setupGeneralBinaryOp(GeneralBinaryOp gbo) {
 482         int primID = gbo.getPrimTypeID();
 483         String methodSignature = gbo.getSignature();
 484         SurfaceType srctype = gbo.getSourceType();
 485         CompositeType comptype = gbo.getCompositeType();
 486         SurfaceType dsttype = gbo.getDestType();
 487         Blit convertsrc, convertdst, convertres;
 488         GraphicsPrimitive performop;
 489 
 490         convertsrc = createConverter(srctype, SurfaceType.IntArgb);
 491         performop = GraphicsPrimitiveMgr.locatePrim(primID,
 492                                                     SurfaceType.IntArgb,
 493                                                     comptype, dsttype);
 494         if (performop != null) {
 495             convertdst = null;
 496             convertres = null;
 497         } else {
 498             performop = getGeneralOp(primID, comptype);
 499             if (performop == null) {
 500                 throw new InternalError("Cannot construct general op for "+




  23  * questions.
  24  */
  25 
  26 /*
  27  * @author Charlton Innovations, Inc.
  28  */
  29 
  30 package sun.java2d.loops;
  31 
  32 import java.awt.image.BufferedImage;
  33 import java.awt.AlphaComposite;
  34 import java.awt.Rectangle;
  35 import sun.awt.image.BufImgSurfaceData;
  36 import sun.awt.util.ThreadGroupUtils;
  37 import sun.java2d.SurfaceData;
  38 import sun.java2d.pipe.Region;
  39 import java.lang.reflect.Field;
  40 import java.util.StringTokenizer;
  41 import java.util.Iterator;
  42 import java.util.HashMap;
  43 import java.util.HashSet;
  44 import java.util.Map;
  45 import java.io.PrintStream;
  46 import java.io.OutputStream;
  47 import java.io.FileOutputStream;
  48 import java.io.FileNotFoundException;
  49 import java.security.AccessController;
  50 import java.security.PrivilegedAction;
  51 
  52 import sun.security.action.GetPropertyAction;
  53 
  54 /**
  55  * defines interface for primitives which can be placed into
  56  * the graphic component manager framework
  57  */
  58 public abstract class GraphicsPrimitive {
  59 
  60     protected static interface GeneralBinaryOp {
  61         /**
  62          * This method allows the setupGeneralBinaryOp method to set
  63          * the converters into the General version of the Primitive.


 306     //
 307     // A version of satisfies used for regression testing
 308     //
 309     final boolean satisfiesSameAs(GraphicsPrimitive other) {
 310         return (methodSignature == other.methodSignature &&
 311                 sourceType.equals(other.sourceType) &&
 312                 compositeType.equals(other.compositeType) &&
 313                 destType.equals(other.destType));
 314     }
 315 
 316     public abstract GraphicsPrimitive makePrimitive(SurfaceType srctype,
 317                                                     CompositeType comptype,
 318                                                     SurfaceType dsttype);
 319 
 320     public abstract GraphicsPrimitive traceWrap();
 321 
 322     static HashMap<Object, int[]> traceMap;
 323 
 324     public static int traceflags;
 325     public static String tracefile;
 326     public static String pname;
 327     public static PrintStream traceout;
 328     public static long treshold = 0;
 329     public static boolean verbose = false;
 330 
 331     public static final int TRACELOG = 1;
 332     public static final int TRACETIMESTAMP = 2;
 333     public static final int TRACECOUNTS = 4;
 334     public static final int TRACEPTIME = 8;
 335     public static final int TRACEPNAME = 16;
 336     public static final int TRACEPIMPL = 32;
 337 
 338     static void showTraceUsage() {
 339         System.err.println("usage: -Dsun.java2d.trace="+
 340                 "[log[,timestamp]],[count],[ptime],[pimpl],[name:<substr pattern>],"+
 341                 "[out:<filename>],[td=<treshold>],[help],[verbose]");
 342     }
 343 
 344     static {
 345         GetPropertyAction gpa = new GetPropertyAction("sun.java2d.trace");
 346         String trace = AccessController.doPrivileged(gpa);
 347         if (trace != null) {

 348             int traceflags = 0;
 349             StringTokenizer st = new StringTokenizer(trace, ",");
 350             while (st.hasMoreTokens()) {
 351                 String tok = st.nextToken();
 352                 if (tok.equalsIgnoreCase("count")) {
 353                     traceflags |= GraphicsPrimitive.TRACECOUNTS;
 354                 } else if (tok.equalsIgnoreCase("log")) {
 355                     traceflags |= GraphicsPrimitive.TRACELOG;
 356                 } else if (tok.equalsIgnoreCase("timestamp")) {
 357                     traceflags |= GraphicsPrimitive.TRACETIMESTAMP;
 358                 } else if (tok.equalsIgnoreCase("ptime")) {
 359                     traceflags |=GraphicsPrimitive.TRACEPTIME;
 360                 } else if (tok.equalsIgnoreCase("pimpl")) {
 361                     traceflags |=GraphicsPrimitive.TRACEPIMPL;
 362                 } else if (tok.regionMatches(true, 0, "name:", 0, 5)) {
 363                     traceflags |=GraphicsPrimitive.TRACEPNAME;
 364                     pname = tok.substring(6);
 365                 } else if (tok.equalsIgnoreCase("verbose")) {
 366                     verbose = true;
 367                 } else if (tok.regionMatches(true, 0, "out:", 0, 4)) {
 368                     tracefile = tok.substring(4);
 369                 } else if (tok.regionMatches(true, 0, "td=", 0, 3)) {
 370                     try {
 371                         treshold = Long.parseLong(tok.substring(3));
 372                     } catch (NumberFormatException e) {
 373                         showTraceUsage();
 374                     }
 375                 } else {
 376                     if (!tok.equalsIgnoreCase("help")) {
 377                         System.err.println("unrecognized token: "+tok);
 378                     }
 379                     showTraceUsage();


 380                 }
 381             }
 382 
 383             GraphicsPrimitiveMgr.setTraceFlags(traceflags);
 384 
 385             if (verbose) {
 386                 System.err.print("GraphicsPrimitive logging ");
 387                 if ((traceflags & GraphicsPrimitive.TRACELOG) != 0) {
 388                     System.err.println("enabled");
 389                     System.err.print("GraphicsPrimitive timetamps ");
 390                     if ((traceflags & GraphicsPrimitive.TRACETIMESTAMP) != 0) {
 391                         System.err.println("enabled");
 392                     } else {
 393                         System.err.println("disabled");
 394                     }
 395                 } else {
 396                     System.err.println("[and timestamps] disabled");
 397                 }
 398                 System.err.print("GraphicsPrimitive invocation counts ");
 399                 if ((traceflags & GraphicsPrimitive.TRACECOUNTS) != 0) {
 400                     System.err.println("enabled");
 401                 } else {
 402                     System.err.println("disabled");
 403                 }
 404                 System.err.print("GraphicsPrimitive trace output to ");


 425                             try {
 426                                 return new FileOutputStream(tracefile);
 427                             } catch (FileNotFoundException e) {
 428                                 return null;
 429                             }
 430                         }
 431                     });
 432                 if (o != null) {
 433                     traceout = new PrintStream(o);
 434                 } else {
 435                     traceout = System.err;
 436                 }
 437             } else {
 438                 traceout = System.err;
 439             }
 440         }
 441         return traceout;
 442     }
 443 
 444     public static class TraceReporter implements Runnable {
 445         private static boolean hookEnabled = false;
 446 
 447         public static synchronized void setShutdownHook() {
 448             if (hookEnabled) return;
 449             hookEnabled = true;
 450 
 451             AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 452                 TraceReporter t = new TraceReporter();
 453                 Thread thread = new Thread(
 454                         ThreadGroupUtils.getRootThreadGroup(), t,
 455                         "TraceReporter", 0, false);
 456                 thread.setContextClassLoader(null);
 457                 Runtime.getRuntime().addShutdownHook(thread);
 458                 return null;
 459             });
 460         }
 461 
 462         public void run() {
 463             PrintStream ps = getTraceOutputFile();
 464             if (traceMap != null) {
 465                 Iterator<Map.Entry<Object, int[]>> iterator =
 466                         traceMap.entrySet().iterator();
 467                 long total = 0;
 468                 int numprims = 0;
 469                 while (iterator.hasNext()) {
 470                     Map.Entry<Object, int[]> me = iterator.next();
 471                     Object prim = me.getKey();
 472                     int[] count = me.getValue();
 473                     if (count[0] == 1) {
 474                         ps.print("1 call to ");
 475                     } else {
 476                         ps.print(count[0] + " calls to ");
 477                     }
 478                     ps.println(prim);
 479                     numprims++;
 480                     total += count[0];
 481                 }
 482                 if (numprims == 0) {
 483                     ps.println("No graphics primitives executed");
 484                 } else if (numprims > 1) {
 485                     ps.println(total + " total calls to " +
 486                             numprims + " different primitives");
 487                 }
 488             }
 489         }
 490     }
 491 
 492     public synchronized static void tracePrimitive(Object prim) {
 493         if ((traceflags & TRACEPNAME) != 0) {
 494             if (!prim.toString().contains(pname)) return;
 495         }
 496 

 497         if ((traceflags & TRACECOUNTS) != 0) {
 498             if (traceMap == null) {
 499                 traceMap = new HashMap<>();
 500                 TraceReporter.setShutdownHook();
 501             }
 502             int[] o = traceMap.get(prim);
 503             if (o == null) {
 504                 o = new int[1];
 505                 traceMap.put(prim, o);
 506             }
 507             o[0]++;
 508         }
 509         if ((traceflags & TRACELOG) != 0) {
 510             PrintStream ps = getTraceOutputFile();
 511             if ((traceflags & TRACETIMESTAMP) != 0) {
 512                 ps.print(System.currentTimeMillis()+": ");
 513             }
 514             ps.println(prim);
 515         }
 516     }
 517 
 518     public synchronized static void traceImplPrimitive(Object prim, Object msg) {
 519         if ((traceflags & TRACEPNAME) != 0) {
 520             if (!prim.toString().contains(pname)) return;
 521         }
 522 
 523         if ((traceflags & TRACEPIMPL) != 0) {
 524             PrintStream ps = getTraceOutputFile();
 525             if ((traceflags & TRACETIMESTAMP) != 0) {
 526                 ps.print(System.currentTimeMillis());
 527             }
 528             ps.println(prim + " : " + msg);
 529         }
 530     }
 531 
 532 
 533     public synchronized static void tracePrimitiveTime(Object prim, long time) {
 534         if ((traceflags & TRACEPNAME) != 0) {
 535             if (!prim.toString().contains(pname)) return;
 536         }
 537         if (time > treshold && (traceflags & TRACEPTIME) != 0  && (traceflags & TRACELOG) != 0) {
 538             PrintStream ps = getTraceOutputFile();
 539             ps.println(prim + " time: " + time);
 540             if (verbose) {
 541                 final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
 542                 if (stackTrace.length > 3) {
 543                     for (int i = 3; i < stackTrace.length; i++) {
 544                         ps.println("  " + stackTrace[i].toString());
 545                     }
 546                 }
 547                 ps.println();
 548             }
 549         }
 550     }
 551 
 552     protected void setupGeneralBinaryOp(GeneralBinaryOp gbo) {
 553         int primID = gbo.getPrimTypeID();
 554         String methodSignature = gbo.getSignature();
 555         SurfaceType srctype = gbo.getSourceType();
 556         CompositeType comptype = gbo.getCompositeType();
 557         SurfaceType dsttype = gbo.getDestType();
 558         Blit convertsrc, convertdst, convertres;
 559         GraphicsPrimitive performop;
 560 
 561         convertsrc = createConverter(srctype, SurfaceType.IntArgb);
 562         performop = GraphicsPrimitiveMgr.locatePrim(primID,
 563                                                     SurfaceType.IntArgb,
 564                                                     comptype, dsttype);
 565         if (performop != null) {
 566             convertdst = null;
 567             convertres = null;
 568         } else {
 569             performop = getGeneralOp(primID, comptype);
 570             if (performop == null) {
 571                 throw new InternalError("Cannot construct general op for "+


< prev index next >