1 /* 2 * Copyright (c) 1997, 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 /* 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. 64 */ 65 public void setPrimitives(Blit srcconverter, 66 Blit dstconverter, 67 GraphicsPrimitive genericop, 68 Blit resconverter); 69 70 /** 71 * These 4 methods are implemented automatically for any 72 * GraphicsPrimitive. They are used by setupGeneralBinaryOp 73 * to retrieve the information needed to find the right 74 * converter primitives. 75 */ 76 public SurfaceType getSourceType(); 77 public CompositeType getCompositeType(); 78 public SurfaceType getDestType(); 79 public String getSignature(); 80 public int getPrimTypeID(); 81 } 82 83 protected static interface GeneralUnaryOp { 84 /** 85 * This method allows the setupGeneralUnaryOp method to set 86 * the converters into the General version of the Primitive. 87 */ 88 public void setPrimitives(Blit dstconverter, 89 GraphicsPrimitive genericop, 90 Blit resconverter); 91 92 /** 93 * These 3 methods are implemented automatically for any 94 * GraphicsPrimitive. They are used by setupGeneralUnaryOp 95 * to retrieve the information needed to find the right 96 * converter primitives. 97 */ 98 public CompositeType getCompositeType(); 99 public SurfaceType getDestType(); 100 public String getSignature(); 101 public int getPrimTypeID(); 102 } 103 104 /** 105 * INSTANCE DATA MEMBERS DESCRIBING CHARACTERISTICS OF THIS PRIMITIVE 106 **/ 107 108 // Making these be instance data members (instead of virtual methods 109 // overridden by subclasses) is actually cheaper, since each class 110 // is a singleton. As instance data members with final accessors, 111 // accesses can be inlined. 112 private String methodSignature; 113 private int uniqueID; 114 private static int unusedPrimID = 1; 115 116 private SurfaceType sourceType; 117 private CompositeType compositeType; 118 private SurfaceType destType; 119 120 private long pNativePrim; // Native blit loop info 121 122 public static final synchronized int makePrimTypeID() { 123 if (unusedPrimID > 255) { 124 throw new InternalError("primitive id overflow"); 125 } 126 return unusedPrimID++; 127 } 128 129 public static final synchronized int makeUniqueID(int primTypeID, 130 SurfaceType src, 131 CompositeType cmp, 132 SurfaceType dst) 133 { 134 return (primTypeID << 24) | 135 (dst.getUniqueID() << 16) | 136 (cmp.getUniqueID() << 8) | 137 (src.getUniqueID()); 138 } 139 140 /** 141 * Create a new GraphicsPrimitive with all of the required 142 * descriptive information. 143 */ 144 protected GraphicsPrimitive(String methodSignature, 145 int primTypeID, 146 SurfaceType sourceType, 147 CompositeType compositeType, 148 SurfaceType destType) 149 { 150 this.methodSignature = methodSignature; 151 this.sourceType = sourceType; 152 this.compositeType = compositeType; 153 this.destType = destType; 154 155 if(sourceType == null || compositeType == null || destType == null) { 156 this.uniqueID = primTypeID << 24; 157 } else { 158 this.uniqueID = GraphicsPrimitive.makeUniqueID(primTypeID, 159 sourceType, 160 compositeType, 161 destType); 162 } 163 } 164 165 /** 166 * Create a new GraphicsPrimitive for native invocation 167 * with all of the required descriptive information. 168 */ 169 protected GraphicsPrimitive(long pNativePrim, 170 String methodSignature, 171 int primTypeID, 172 SurfaceType sourceType, 173 CompositeType compositeType, 174 SurfaceType destType) 175 { 176 this.pNativePrim = pNativePrim; 177 this.methodSignature = methodSignature; 178 this.sourceType = sourceType; 179 this.compositeType = compositeType; 180 this.destType = destType; 181 182 if(sourceType == null || compositeType == null || destType == null) { 183 this.uniqueID = primTypeID << 24; 184 } else { 185 this.uniqueID = GraphicsPrimitive.makeUniqueID(primTypeID, 186 sourceType, 187 compositeType, 188 destType); 189 } 190 } 191 192 /** 193 * METHODS TO DESCRIBE THE SURFACES PRIMITIVES 194 * CAN OPERATE ON AND THE FUNCTIONALITY THEY IMPLEMENT 195 **/ 196 197 /** 198 * Gets instance ID of this graphics primitive. 199 * 200 * Instance ID is comprised of four distinct ids (ORed together) 201 * that uniquely identify each instance of a GraphicsPrimitive 202 * object. The four ids making up instance ID are: 203 * 1. primitive id - identifier shared by all primitives of the 204 * same type (eg. all Blits have the same primitive id) 205 * 2. sourcetype id - identifies source surface type 206 * 3. desttype id - identifies destination surface type 207 * 4. compositetype id - identifies composite used 208 * 209 * @return instance ID 210 */ 211 public final int getUniqueID() { 212 return uniqueID; 213 } 214 215 /** 216 */ 217 public final String getSignature() { 218 return methodSignature; 219 } 220 221 /** 222 * Gets unique id for this GraphicsPrimitive type. 223 * 224 * This id is used to identify the TYPE of primitive (Blit vs. BlitBg) 225 * as opposed to INSTANCE of primitive. 226 * 227 * @return primitive ID 228 */ 229 public final int getPrimTypeID() { 230 return uniqueID >>> 24; 231 } 232 233 /** 234 */ 235 public final long getNativePrim() { 236 return pNativePrim; 237 } 238 239 /** 240 */ 241 public final SurfaceType getSourceType() { 242 return sourceType; 243 } 244 245 /** 246 */ 247 public final CompositeType getCompositeType() { 248 return compositeType; 249 } 250 251 /** 252 */ 253 public final SurfaceType getDestType() { 254 return destType; 255 } 256 257 /** 258 * Return true if this primitive can be used for the given signature 259 * surfaces, and composite. 260 * 261 * @param signature The signature of the given operation. Must be 262 * == (not just .equals) the signature string given by the 263 * abstract class that declares the operation. 264 * @param srctype The surface type for the source of the operation 265 * @param comptype The composite type for the operation 266 * @param dsttype The surface type for the destination of the operation 267 */ 268 public final boolean satisfies(String signature, 269 SurfaceType srctype, 270 CompositeType comptype, 271 SurfaceType dsttype) 272 { 273 if (signature != methodSignature) { 274 return false; 275 } 276 while (true) { 277 if (srctype == null) { 278 return false; 279 } 280 if (srctype.equals(sourceType)) { 281 break; 282 } 283 srctype = srctype.getSuperType(); 284 } 285 while (true) { 286 if (comptype == null) { 287 return false; 288 } 289 if (comptype.equals(compositeType)) { 290 break; 291 } 292 comptype = comptype.getSuperType(); 293 } 294 while (true) { 295 if (dsttype == null) { 296 return false; 297 } 298 if (dsttype.equals(destType)) { 299 break; 300 } 301 dsttype = dsttype.getSuperType(); 302 } 303 return true; 304 } 305 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 "); 405 if (tracefile == null) { 406 System.err.println("System.err"); 407 } else { 408 System.err.println("file '"+tracefile+"'"); 409 } 410 } 411 GraphicsPrimitive.traceflags = traceflags; 412 } 413 } 414 415 public static boolean tracingEnabled() { 416 return (traceflags != 0); 417 } 418 419 private static PrintStream getTraceOutputFile() { 420 if (traceout == null) { 421 if (tracefile != null) { 422 FileOutputStream o = AccessController.doPrivileged( 423 new PrivilegedAction<FileOutputStream>() { 424 public FileOutputStream run() { 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 "+ 572 methodSignature+" "+comptype); 573 } 574 convertdst = createConverter(dsttype, SurfaceType.IntArgb); 575 convertres = createConverter(SurfaceType.IntArgb, dsttype); 576 } 577 578 gbo.setPrimitives(convertsrc, convertdst, performop, convertres); 579 } 580 581 protected void setupGeneralUnaryOp(GeneralUnaryOp guo) { 582 int primID = guo.getPrimTypeID(); 583 String methodSignature = guo.getSignature(); 584 CompositeType comptype = guo.getCompositeType(); 585 SurfaceType dsttype = guo.getDestType(); 586 587 Blit convertdst = createConverter(dsttype, SurfaceType.IntArgb); 588 GraphicsPrimitive performop = getGeneralOp(primID, comptype); 589 Blit convertres = createConverter(SurfaceType.IntArgb, dsttype); 590 if (convertdst == null || performop == null || convertres == null) { 591 throw new InternalError("Cannot construct binary op for "+ 592 comptype+" "+dsttype); 593 } 594 595 guo.setPrimitives(convertdst, performop, convertres); 596 } 597 598 protected static Blit createConverter(SurfaceType srctype, 599 SurfaceType dsttype) 600 { 601 if (srctype.equals(dsttype)) { 602 return null; 603 } 604 Blit cv = Blit.getFromCache(srctype, CompositeType.SrcNoEa, dsttype); 605 if (cv == null) { 606 throw new InternalError("Cannot construct converter for "+ 607 srctype+"=>"+dsttype); 608 } 609 return cv; 610 } 611 612 protected static SurfaceData convertFrom(Blit ob, SurfaceData srcData, 613 int srcX, int srcY, int w, int h, 614 SurfaceData dstData) 615 { 616 return convertFrom(ob, srcData, 617 srcX, srcY, w, h, dstData, 618 BufferedImage.TYPE_INT_ARGB); 619 } 620 621 protected static SurfaceData convertFrom(Blit ob, SurfaceData srcData, 622 int srcX, int srcY, int w, int h, 623 SurfaceData dstData, int type) 624 { 625 if (dstData != null) { 626 Rectangle r = dstData.getBounds(); 627 if (w > r.width || h > r.height) { 628 dstData = null; 629 } 630 } 631 if (dstData == null) { 632 BufferedImage dstBI = new BufferedImage(w, h, type); 633 dstData = BufImgSurfaceData.createData(dstBI); 634 } 635 ob.Blit(srcData, dstData, AlphaComposite.Src, null, 636 srcX, srcY, 0, 0, w, h); 637 return dstData; 638 } 639 640 protected static void convertTo(Blit ob, 641 SurfaceData srcImg, SurfaceData dstImg, 642 Region clip, 643 int dstX, int dstY, int w, int h) 644 { 645 if (ob != null) { 646 ob.Blit(srcImg, dstImg, AlphaComposite.Src, clip, 647 0, 0, dstX, dstY, w, h); 648 } 649 } 650 651 protected static GraphicsPrimitive getGeneralOp(int primID, 652 CompositeType comptype) 653 { 654 return GraphicsPrimitiveMgr.locatePrim(primID, 655 SurfaceType.IntArgb, 656 comptype, 657 SurfaceType.IntArgb); 658 } 659 660 public static String simplename(Field[] fields, Object o) { 661 for (int i = 0; i < fields.length; i++) { 662 Field f = fields[i]; 663 try { 664 if (o == f.get(null)) { 665 return f.getName(); 666 } 667 } catch (Exception e) { 668 } 669 } 670 return "\""+o.toString()+"\""; 671 } 672 673 public static String simplename(SurfaceType st) { 674 return simplename(SurfaceType.class.getDeclaredFields(), st); 675 } 676 677 public static String simplename(CompositeType ct) { 678 return simplename(CompositeType.class.getDeclaredFields(), ct); 679 } 680 681 private String cachedname; 682 683 public String toString() { 684 if (cachedname == null) { 685 String sig = methodSignature; 686 int index = sig.indexOf('('); 687 if (index >= 0) { 688 sig = sig.substring(0, index); 689 } 690 cachedname = (getClass().getName()+"::"+ 691 sig+"("+ 692 simplename(sourceType)+", "+ 693 simplename(compositeType)+", "+ 694 simplename(destType)+")"); 695 } 696 return cachedname; 697 } 698 }