68 }, 69 OFF{ 70 @Override 71 PathIterator getNormalizingPathIterator(final RendererContext rdrCtx, 72 final PathIterator src) 73 { 74 // return original path iterator if normalization is disabled: 75 return src; 76 } 77 }; 78 79 abstract PathIterator getNormalizingPathIterator(RendererContext rdrCtx, 80 PathIterator src); 81 } 82 83 private static final float MIN_PEN_SIZE = 1.0f / NORM_SUBPIXELS; 84 85 static final float UPPER_BND = Float.MAX_VALUE / 2.0f; 86 static final float LOWER_BND = -UPPER_BND; 87 88 /** 89 * Public constructor 90 */ 91 public MarlinRenderingEngine() { 92 super(); 93 logSettings(MarlinRenderingEngine.class.getName()); 94 } 95 96 /** 97 * Create a widened path as specified by the parameters. 98 * <p> 99 * The specified {@code src} {@link Shape} is widened according 100 * to the specified attribute parameters as per the 101 * {@link BasicStroke} specification. 102 * 103 * @param src the source path to be widened 104 * @param width the width of the widened path as per {@code BasicStroke} 105 * @param caps the end cap decorations as per {@code BasicStroke} 106 * @param join the segment join decorations as per {@code BasicStroke} 107 * @param miterlimit the miter limit as per {@code BasicStroke} 307 float dashphase, 308 PathConsumer2D pc2d) 309 { 310 // We use strokerat so that in Stroker and Dasher we can work only 311 // with the pre-transformation coordinates. This will repeat a lot of 312 // computations done in the path iterator, but the alternative is to 313 // work with transformed paths and compute untransformed coordinates 314 // as needed. This would be faster but I do not think the complexity 315 // of working with both untransformed and transformed coordinates in 316 // the same code is worth it. 317 // However, if a path's width is constant after a transformation, 318 // we can skip all this untransforming. 319 320 // As pathTo() will check transformed coordinates for invalid values 321 // (NaN / Infinity) to ignore such points, it is necessary to apply the 322 // transformation before the path processing. 323 AffineTransform strokerat = null; 324 325 int dashLen = -1; 326 boolean recycleDashes = false; 327 328 if (at != null && !at.isIdentity()) { 329 final double a = at.getScaleX(); 330 final double b = at.getShearX(); 331 final double c = at.getShearY(); 332 final double d = at.getScaleY(); 333 final double det = a * d - c * b; 334 335 if (Math.abs(det) <= (2.0f * Float.MIN_VALUE)) { 336 // this rendering engine takes one dimensional curves and turns 337 // them into 2D shapes by giving them width. 338 // However, if everything is to be passed through a singular 339 // transformation, these 2D shapes will be squashed down to 1D 340 // again so, nothing can be drawn. 341 342 // Every path needs an initial moveTo and a pathDone. If these 343 // are not there this causes a SIGSEGV in libawt.so (at the time 344 // of writing of this comment (September 16, 2010)). Actually, 345 // I am not sure if the moveTo is necessary to avoid the SIGSEGV 346 // but the pathDone is definitely needed. 347 pc2d.moveTo(0.0f, 0.0f); 348 pc2d.pathDone(); 349 return; 350 } 351 352 // If the transform is a constant multiple of an orthogonal transformation 353 // then every length is just multiplied by a constant, so we just 354 // need to transform input paths to stroker and tell stroker 355 // the scaled width. This condition is satisfied if 356 // a*b == -c*d && a*a+c*c == b*b+d*d. In the actual check below, we 357 // leave a bit of room for error. 358 if (nearZero(a*b + c*d) && nearZero(a*a + c*c - (b*b + d*d))) { 359 final float scale = (float) Math.sqrt(a*a + c*c); 360 361 if (dashes != null) { 362 recycleDashes = true; 363 dashLen = dashes.length; 364 dashes = rdrCtx.dasher.copyDashArray(dashes); 365 for (int i = 0; i < dashLen; i++) { 366 dashes[i] *= scale; 367 } 368 dashphase *= scale; 369 } 370 width *= scale; 371 372 // by now strokerat == null. Input paths to 373 // stroker (and maybe dasher) will have the full transform at 374 // applied to them and nothing will happen to the output paths. 375 } else { 376 strokerat = at; 377 378 // by now strokerat == at. Input paths to 379 // stroker (and maybe dasher) will have the full transform at 380 // applied to them, then they will be normalized, and then 381 // the inverse of *only the non translation part of at* will 382 // be applied to the normalized paths. This won't cause problems 383 // in stroker, because, suppose at = T*A, where T is just the 384 // translation part of at, and A is the rest. T*A has already 385 // been applied to Stroker/Dasher's input. Then Ainv will be 386 // applied. Ainv*T*A is not equal to T, but it is a translation, 387 // which means that none of stroker's assumptions about its 388 // input will be violated. After all this, A will be applied 389 // to stroker's output. 390 } 391 } else { 392 // either at is null or it's the identity. In either case 393 // we don't transform the path. 394 at = null; 395 } 396 397 if (USE_SIMPLIFIER) { 398 // Use simplifier after stroker before Renderer 399 // to remove collinear segments (notably due to cap square) 400 pc2d = rdrCtx.simplifier.init(pc2d); 401 } 402 403 final TransformingPathConsumer2D transformerPC2D = rdrCtx.transformerPC2D; 404 pc2d = transformerPC2D.deltaTransformConsumer(pc2d, strokerat); 405 406 pc2d = rdrCtx.stroker.init(pc2d, width, caps, join, miterlimit); 407 408 if (dashes != null) { 409 if (!recycleDashes) { 410 dashLen = dashes.length; 411 } 412 pc2d = rdrCtx.dasher.init(pc2d, dashes, dashLen, dashphase, 413 recycleDashes); 414 } 415 pc2d = transformerPC2D.inverseDeltaTransformConsumer(pc2d, strokerat); 416 417 final PathIterator pi = norm.getNormalizingPathIterator(rdrCtx, 418 src.getPathIterator(at)); 419 420 pathTo(rdrCtx, pi, pc2d); 421 422 /* 423 * Pipeline seems to be: 424 * shape.getPathIterator(at) 425 * -> (NormalizingPathIterator) 426 * -> (inverseDeltaTransformConsumer) 427 * -> (Dasher) 428 * -> Stroker 429 * -> (deltaTransformConsumer) 430 * 431 * -> (CollinearSimplifier) to remove redundant segments 432 * 433 * -> pc2d = Renderer (bounding box) 434 */ 435 } 436 786 final NormMode norm = (normalize) ? NormMode.ON_WITH_AA : NormMode.OFF; 787 788 if (bs == null) { 789 // fill shape: 790 final PathIterator pi = norm.getNormalizingPathIterator(rdrCtx, 791 s.getPathIterator(_at)); 792 793 // note: Winding rule may be EvenOdd ONLY for fill operations ! 794 r = rdrCtx.renderer.init(clip.getLoX(), clip.getLoY(), 795 clip.getWidth(), clip.getHeight(), 796 pi.getWindingRule()); 797 798 // TODO: subdivide quad/cubic curves into monotonic curves ? 799 pathTo(rdrCtx, pi, r); 800 } else { 801 // draw shape with given stroke: 802 r = rdrCtx.renderer.init(clip.getLoX(), clip.getLoY(), 803 clip.getWidth(), clip.getHeight(), 804 PathIterator.WIND_NON_ZERO); 805 806 strokeTo(rdrCtx, s, _at, bs, thin, norm, true, r); 807 } 808 if (r.endRendering()) { 809 ptg = rdrCtx.ptg.init(); 810 ptg.getBbox(bbox); 811 // note: do not returnRendererContext(rdrCtx) 812 // as it will be called later by MarlinTileGenerator.dispose() 813 r = null; 814 } 815 } finally { 816 if (r != null) { 817 // dispose renderer and recycle the RendererContext instance: 818 r.dispose(); 819 } 820 } 821 822 // Return null to cancel AA tile generation (nothing to render) 823 return ptg; 824 } 825 843 x -= (ldx1 + ldx2) / 2.0d; 844 y -= (ldy1 + ldy2) / 2.0d; 845 dx1 += ldx1; 846 dy1 += ldy1; 847 dx2 += ldx2; 848 dy2 += ldy2; 849 if (lw1 > 1.0d && lw2 > 1.0d) { 850 // Inner parallelogram was entirely consumed by stroke... 851 innerpgram = false; 852 } 853 } else { 854 ldx1 = ldy1 = ldx2 = ldy2 = 0.0d; 855 } 856 857 MarlinTileGenerator ptg = null; 858 Renderer r = null; 859 860 final RendererContext rdrCtx = getRendererContext(); 861 try { 862 r = rdrCtx.renderer.init(clip.getLoX(), clip.getLoY(), 863 clip.getWidth(), clip.getHeight(), 864 Renderer.WIND_EVEN_ODD); 865 866 r.moveTo((float) x, (float) y); 867 r.lineTo((float) (x+dx1), (float) (y+dy1)); 868 r.lineTo((float) (x+dx1+dx2), (float) (y+dy1+dy2)); 869 r.lineTo((float) (x+dx2), (float) (y+dy2)); 870 r.closePath(); 871 872 if (innerpgram) { 873 x += ldx1 + ldx2; 874 y += ldy1 + ldy2; 875 dx1 -= 2.0d * ldx1; 876 dy1 -= 2.0d * ldy1; 877 dx2 -= 2.0d * ldx2; 878 dy2 -= 2.0d * ldy2; 879 r.moveTo((float) x, (float) y); 880 r.lineTo((float) (x+dx1), (float) (y+dy1)); 881 r.lineTo((float) (x+dx1+dx2), (float) (y+dy1+dy2)); 882 r.lineTo((float) (x+dx2), (float) (y+dy2)); 883 r.closePath(); 884 } 1028 + MarlinConst.TILE_W_LG); 1029 logInfo("sun.java2d.renderer.blockSize_log2 = " 1030 + MarlinConst.BLOCK_SIZE_LG); 1031 1032 // RLE / blockFlags settings 1033 1034 logInfo("sun.java2d.renderer.forceRLE = " 1035 + MarlinProperties.isForceRLE()); 1036 logInfo("sun.java2d.renderer.forceNoRLE = " 1037 + MarlinProperties.isForceNoRLE()); 1038 logInfo("sun.java2d.renderer.useTileFlags = " 1039 + MarlinProperties.isUseTileFlags()); 1040 logInfo("sun.java2d.renderer.useTileFlags.useHeuristics = " 1041 + MarlinProperties.isUseTileFlagsWithHeuristics()); 1042 logInfo("sun.java2d.renderer.rleMinWidth = " 1043 + MarlinCache.RLE_MIN_WIDTH); 1044 1045 // optimisation parameters 1046 logInfo("sun.java2d.renderer.useSimplifier = " 1047 + MarlinConst.USE_SIMPLIFIER); 1048 1049 // debugging parameters 1050 logInfo("sun.java2d.renderer.doStats = " 1051 + MarlinConst.DO_STATS); 1052 logInfo("sun.java2d.renderer.doMonitors = " 1053 + MarlinConst.DO_MONITORS); 1054 logInfo("sun.java2d.renderer.doChecks = " 1055 + MarlinConst.DO_CHECKS); 1056 1057 // logging parameters 1058 logInfo("sun.java2d.renderer.useLogger = " 1059 + MarlinConst.USE_LOGGER); 1060 logInfo("sun.java2d.renderer.logCreateContext = " 1061 + MarlinConst.LOG_CREATE_CONTEXT); 1062 logInfo("sun.java2d.renderer.logUnsafeMalloc = " 1063 + MarlinConst.LOG_UNSAFE_MALLOC); 1064 1065 // quality settings 1066 logInfo("sun.java2d.renderer.cubic_dec_d2 = " 1067 + MarlinProperties.getCubicDecD2()); | 68 }, 69 OFF{ 70 @Override 71 PathIterator getNormalizingPathIterator(final RendererContext rdrCtx, 72 final PathIterator src) 73 { 74 // return original path iterator if normalization is disabled: 75 return src; 76 } 77 }; 78 79 abstract PathIterator getNormalizingPathIterator(RendererContext rdrCtx, 80 PathIterator src); 81 } 82 83 private static final float MIN_PEN_SIZE = 1.0f / NORM_SUBPIXELS; 84 85 static final float UPPER_BND = Float.MAX_VALUE / 2.0f; 86 static final float LOWER_BND = -UPPER_BND; 87 88 static final boolean DO_CLIP = MarlinProperties.isDoClip(); 89 static final boolean DO_TRACE = false; 90 91 /** 92 * Public constructor 93 */ 94 public MarlinRenderingEngine() { 95 super(); 96 logSettings(MarlinRenderingEngine.class.getName()); 97 } 98 99 /** 100 * Create a widened path as specified by the parameters. 101 * <p> 102 * The specified {@code src} {@link Shape} is widened according 103 * to the specified attribute parameters as per the 104 * {@link BasicStroke} specification. 105 * 106 * @param src the source path to be widened 107 * @param width the width of the widened path as per {@code BasicStroke} 108 * @param caps the end cap decorations as per {@code BasicStroke} 109 * @param join the segment join decorations as per {@code BasicStroke} 110 * @param miterlimit the miter limit as per {@code BasicStroke} 310 float dashphase, 311 PathConsumer2D pc2d) 312 { 313 // We use strokerat so that in Stroker and Dasher we can work only 314 // with the pre-transformation coordinates. This will repeat a lot of 315 // computations done in the path iterator, but the alternative is to 316 // work with transformed paths and compute untransformed coordinates 317 // as needed. This would be faster but I do not think the complexity 318 // of working with both untransformed and transformed coordinates in 319 // the same code is worth it. 320 // However, if a path's width is constant after a transformation, 321 // we can skip all this untransforming. 322 323 // As pathTo() will check transformed coordinates for invalid values 324 // (NaN / Infinity) to ignore such points, it is necessary to apply the 325 // transformation before the path processing. 326 AffineTransform strokerat = null; 327 328 int dashLen = -1; 329 boolean recycleDashes = false; 330 float scale = 1.0f; 331 332 if (at != null && !at.isIdentity()) { 333 final double a = at.getScaleX(); 334 final double b = at.getShearX(); 335 final double c = at.getShearY(); 336 final double d = at.getScaleY(); 337 final double det = a * d - c * b; 338 339 if (Math.abs(det) <= (2.0f * Float.MIN_VALUE)) { 340 // this rendering engine takes one dimensional curves and turns 341 // them into 2D shapes by giving them width. 342 // However, if everything is to be passed through a singular 343 // transformation, these 2D shapes will be squashed down to 1D 344 // again so, nothing can be drawn. 345 346 // Every path needs an initial moveTo and a pathDone. If these 347 // are not there this causes a SIGSEGV in libawt.so (at the time 348 // of writing of this comment (September 16, 2010)). Actually, 349 // I am not sure if the moveTo is necessary to avoid the SIGSEGV 350 // but the pathDone is definitely needed. 351 pc2d.moveTo(0.0f, 0.0f); 352 pc2d.pathDone(); 353 return; 354 } 355 356 // If the transform is a constant multiple of an orthogonal transformation 357 // then every length is just multiplied by a constant, so we just 358 // need to transform input paths to stroker and tell stroker 359 // the scaled width. This condition is satisfied if 360 // a*b == -c*d && a*a+c*c == b*b+d*d. In the actual check below, we 361 // leave a bit of room for error. 362 if (nearZero(a*b + c*d) && nearZero(a*a + c*c - (b*b + d*d))) { 363 scale = (float) Math.sqrt(a*a + c*c); 364 365 if (dashes != null) { 366 recycleDashes = true; 367 dashLen = dashes.length; 368 dashes = rdrCtx.dasher.copyDashArray(dashes); 369 for (int i = 0; i < dashLen; i++) { 370 dashes[i] *= scale; 371 } 372 dashphase *= scale; 373 } 374 width *= scale; 375 376 // by now strokerat == null. Input paths to 377 // stroker (and maybe dasher) will have the full transform at 378 // applied to them and nothing will happen to the output paths. 379 } else { 380 strokerat = at; 381 382 // by now strokerat == at. Input paths to 383 // stroker (and maybe dasher) will have the full transform at 384 // applied to them, then they will be normalized, and then 385 // the inverse of *only the non translation part of at* will 386 // be applied to the normalized paths. This won't cause problems 387 // in stroker, because, suppose at = T*A, where T is just the 388 // translation part of at, and A is the rest. T*A has already 389 // been applied to Stroker/Dasher's input. Then Ainv will be 390 // applied. Ainv*T*A is not equal to T, but it is a translation, 391 // which means that none of stroker's assumptions about its 392 // input will be violated. After all this, A will be applied 393 // to stroker's output. 394 } 395 } else { 396 // either at is null or it's the identity. In either case 397 // we don't transform the path. 398 at = null; 399 } 400 401 final TransformingPathConsumer2D transformerPC2D = rdrCtx.transformerPC2D; 402 403 if (DO_TRACE) { 404 // trace Stroker: 405 pc2d = transformerPC2D.traceStroker(pc2d); 406 } 407 408 if (USE_SIMPLIFIER) { 409 // Use simplifier after stroker before Renderer 410 // to remove collinear segments (notably due to cap square) 411 pc2d = rdrCtx.simplifier.init(pc2d); 412 } 413 414 // deltaTransformConsumer may adjust the clip rectangle: 415 pc2d = transformerPC2D.deltaTransformConsumer(pc2d, strokerat); 416 417 // stroker will adjust the clip rectangle (width / miter limit): 418 pc2d = rdrCtx.stroker.init(pc2d, width, caps, join, miterlimit, scale); 419 420 if (dashes != null) { 421 if (!recycleDashes) { 422 dashLen = dashes.length; 423 } 424 pc2d = rdrCtx.dasher.init(pc2d, dashes, dashLen, dashphase, 425 recycleDashes); 426 } else if (rdrCtx.doClip && (caps != Stroker.CAP_BUTT)) { 427 if (DO_TRACE) { 428 pc2d = transformerPC2D.traceClosedPathDetector(pc2d); 429 } 430 431 // If no dash and clip is enabled: 432 // detect closedPaths (polygons) for caps 433 pc2d = transformerPC2D.detectClosedPath(pc2d); 434 } 435 pc2d = transformerPC2D.inverseDeltaTransformConsumer(pc2d, strokerat); 436 437 if (DO_TRACE) { 438 // trace Input: 439 pc2d = transformerPC2D.traceInput(pc2d); 440 } 441 442 final PathIterator pi = norm.getNormalizingPathIterator(rdrCtx, 443 src.getPathIterator(at)); 444 445 pathTo(rdrCtx, pi, pc2d); 446 447 /* 448 * Pipeline seems to be: 449 * shape.getPathIterator(at) 450 * -> (NormalizingPathIterator) 451 * -> (inverseDeltaTransformConsumer) 452 * -> (Dasher) 453 * -> Stroker 454 * -> (deltaTransformConsumer) 455 * 456 * -> (CollinearSimplifier) to remove redundant segments 457 * 458 * -> pc2d = Renderer (bounding box) 459 */ 460 } 461 811 final NormMode norm = (normalize) ? NormMode.ON_WITH_AA : NormMode.OFF; 812 813 if (bs == null) { 814 // fill shape: 815 final PathIterator pi = norm.getNormalizingPathIterator(rdrCtx, 816 s.getPathIterator(_at)); 817 818 // note: Winding rule may be EvenOdd ONLY for fill operations ! 819 r = rdrCtx.renderer.init(clip.getLoX(), clip.getLoY(), 820 clip.getWidth(), clip.getHeight(), 821 pi.getWindingRule()); 822 823 // TODO: subdivide quad/cubic curves into monotonic curves ? 824 pathTo(rdrCtx, pi, r); 825 } else { 826 // draw shape with given stroke: 827 r = rdrCtx.renderer.init(clip.getLoX(), clip.getLoY(), 828 clip.getWidth(), clip.getHeight(), 829 PathIterator.WIND_NON_ZERO); 830 831 if (DO_CLIP) { 832 // Define the initial clip bounds: 833 final float[] clipRect = rdrCtx.clipRect; 834 clipRect[0] = clip.getLoY(); 835 clipRect[1] = clip.getLoY() + clip.getHeight(); 836 clipRect[2] = clip.getLoX(); 837 clipRect[3] = clip.getLoX() + clip.getWidth(); 838 839 // Enable clipping: 840 rdrCtx.doClip = true; 841 } 842 843 strokeTo(rdrCtx, s, _at, bs, thin, norm, true, r); 844 } 845 if (r.endRendering()) { 846 ptg = rdrCtx.ptg.init(); 847 ptg.getBbox(bbox); 848 // note: do not returnRendererContext(rdrCtx) 849 // as it will be called later by MarlinTileGenerator.dispose() 850 r = null; 851 } 852 } finally { 853 if (r != null) { 854 // dispose renderer and recycle the RendererContext instance: 855 r.dispose(); 856 } 857 } 858 859 // Return null to cancel AA tile generation (nothing to render) 860 return ptg; 861 } 862 880 x -= (ldx1 + ldx2) / 2.0d; 881 y -= (ldy1 + ldy2) / 2.0d; 882 dx1 += ldx1; 883 dy1 += ldy1; 884 dx2 += ldx2; 885 dy2 += ldy2; 886 if (lw1 > 1.0d && lw2 > 1.0d) { 887 // Inner parallelogram was entirely consumed by stroke... 888 innerpgram = false; 889 } 890 } else { 891 ldx1 = ldy1 = ldx2 = ldy2 = 0.0d; 892 } 893 894 MarlinTileGenerator ptg = null; 895 Renderer r = null; 896 897 final RendererContext rdrCtx = getRendererContext(); 898 try { 899 r = rdrCtx.renderer.init(clip.getLoX(), clip.getLoY(), 900 clip.getWidth(), clip.getHeight(), 901 Renderer.WIND_EVEN_ODD); 902 903 r.moveTo((float) x, (float) y); 904 r.lineTo((float) (x+dx1), (float) (y+dy1)); 905 r.lineTo((float) (x+dx1+dx2), (float) (y+dy1+dy2)); 906 r.lineTo((float) (x+dx2), (float) (y+dy2)); 907 r.closePath(); 908 909 if (innerpgram) { 910 x += ldx1 + ldx2; 911 y += ldy1 + ldy2; 912 dx1 -= 2.0d * ldx1; 913 dy1 -= 2.0d * ldy1; 914 dx2 -= 2.0d * ldx2; 915 dy2 -= 2.0d * ldy2; 916 r.moveTo((float) x, (float) y); 917 r.lineTo((float) (x+dx1), (float) (y+dy1)); 918 r.lineTo((float) (x+dx1+dx2), (float) (y+dy1+dy2)); 919 r.lineTo((float) (x+dx2), (float) (y+dy2)); 920 r.closePath(); 921 } 1065 + MarlinConst.TILE_W_LG); 1066 logInfo("sun.java2d.renderer.blockSize_log2 = " 1067 + MarlinConst.BLOCK_SIZE_LG); 1068 1069 // RLE / blockFlags settings 1070 1071 logInfo("sun.java2d.renderer.forceRLE = " 1072 + MarlinProperties.isForceRLE()); 1073 logInfo("sun.java2d.renderer.forceNoRLE = " 1074 + MarlinProperties.isForceNoRLE()); 1075 logInfo("sun.java2d.renderer.useTileFlags = " 1076 + MarlinProperties.isUseTileFlags()); 1077 logInfo("sun.java2d.renderer.useTileFlags.useHeuristics = " 1078 + MarlinProperties.isUseTileFlagsWithHeuristics()); 1079 logInfo("sun.java2d.renderer.rleMinWidth = " 1080 + MarlinCache.RLE_MIN_WIDTH); 1081 1082 // optimisation parameters 1083 logInfo("sun.java2d.renderer.useSimplifier = " 1084 + MarlinConst.USE_SIMPLIFIER); 1085 logInfo("sun.java2d.renderer.clip = " 1086 + MarlinProperties.isDoClip()); 1087 1088 // debugging parameters 1089 logInfo("sun.java2d.renderer.doStats = " 1090 + MarlinConst.DO_STATS); 1091 logInfo("sun.java2d.renderer.doMonitors = " 1092 + MarlinConst.DO_MONITORS); 1093 logInfo("sun.java2d.renderer.doChecks = " 1094 + MarlinConst.DO_CHECKS); 1095 1096 // logging parameters 1097 logInfo("sun.java2d.renderer.useLogger = " 1098 + MarlinConst.USE_LOGGER); 1099 logInfo("sun.java2d.renderer.logCreateContext = " 1100 + MarlinConst.LOG_CREATE_CONTEXT); 1101 logInfo("sun.java2d.renderer.logUnsafeMalloc = " 1102 + MarlinConst.LOG_UNSAFE_MALLOC); 1103 1104 // quality settings 1105 logInfo("sun.java2d.renderer.cubic_dec_d2 = " 1106 + MarlinProperties.getCubicDecD2()); |