@@ -205,6 +205,8 @@ public void beginShape(int kind) {
205205 workPath .reset ();
206206 auxPath .reset ();
207207
208+ flushPixels ();
209+
208210 if (drawingThinLines ()) {
209211 pushMatrix ();
210212 translate (0.5f , 0.5f );
@@ -412,10 +414,10 @@ public void endShape(int mode) {
412414 }
413415 }
414416 shape = 0 ;
415-
416417 if (drawingThinLines ()) {
417418 popMatrix ();
418419 }
420+ loaded = false ;
419421 }
420422
421423
@@ -627,49 +629,40 @@ public void curveVertex(float x, float y, float z) {
627629
628630 // RENDERER
629631
630-
631632 @ Override
632633 public void flush () {
634+ flushPixels ();
635+ }
636+
637+
638+ protected void flushPixels () {
633639 boolean hasPixels = modified && pixels != null ;
634640 if (hasPixels ) {
635641 // If the user has been manipulating individual pixels,
636642 // the changes need to be copied to the screen before
637643 // drawing any new geometry.
638- flushPixels ();
644+ int mx1 = getModifiedX1 ();
645+ int mx2 = getModifiedX2 ();
646+ int my1 = getModifiedY1 ();
647+ int my2 = getModifiedY2 ();
648+ int mw = mx2 - mx1 ;
649+ int mh = my2 - my1 ;
650+
651+ PixelWriter pw = context .getPixelWriter ();
652+ pw .setPixels (mx1 , my1 , mw , mh , argbFormat , pixels ,
653+ mx1 + my1 * pixelWidth , pixelWidth );
639654 }
640655
641656 modified = false ;
642657 }
643658
644659
645- protected void flushPixels () {
646- int mx1 = getModifiedX1 ();
647- int mx2 = getModifiedX2 ();
648- int my1 = getModifiedY1 ();
649- int my2 = getModifiedY2 ();
650- int mw = mx2 - mx1 ;
651- int mh = my2 - my1 ;
652-
653- checkSnapshotImage ();
654-
655- PixelWriter pw = snapshotImage .getPixelWriter ();
656- pw .setPixels (mx1 , my1 , mw , mh , argbFormat , pixels ,
657- mx1 + my1 * pixelWidth , pixelWidth );
658-
659- context .drawImage (snapshotImage , mx1 , my1 , mw , mh , mx1 , my1 , mw , mh );
660- }
661-
662-
663- protected void checkSnapshotImage () {
664- if (snapshotImage == null ||
665- snapshotImage .getWidth () != pixelWidth ||
666- snapshotImage .getHeight () != pixelHeight ) {
667- snapshotImage = new WritableImage (pixelWidth , pixelHeight );
668- }
660+ protected void beforeContextDraw () {
661+ flushPixels ();
662+ loaded = false ;
669663 }
670664
671665
672-
673666 //////////////////////////////////////////////////////////////
674667
675668 // POINT, LINE, TRIANGLE, QUAD
@@ -689,6 +682,7 @@ public void point(float x, float y) {
689682
690683 @ Override
691684 public void line (float x1 , float y1 , float x2 , float y2 ) {
685+ beforeContextDraw ();
692686 if (drawingThinLines ()) {
693687 x1 += 0.5f ;
694688 x2 += 0.5f ;
@@ -702,6 +696,7 @@ public void line(float x1, float y1, float x2, float y2) {
702696 @ Override
703697 public void triangle (float x1 , float y1 , float x2 , float y2 ,
704698 float x3 , float y3 ) {
699+ beforeContextDraw ();
705700 if (drawingThinLines ()) {
706701 x1 += 0.5f ;
707702 x2 += 0.5f ;
@@ -723,6 +718,7 @@ public void triangle(float x1, float y1, float x2, float y2,
723718 @ Override
724719 public void quad (float x1 , float y1 , float x2 , float y2 ,
725720 float x3 , float y3 , float x4 , float y4 ) {
721+ beforeContextDraw ();
726722 if (drawingThinLines ()) {
727723 x1 += 0.5f ;
728724 x2 += 0.5f ;
@@ -758,8 +754,7 @@ public void quad(float x1, float y1, float x2, float y2,
758754
759755 @ Override
760756 protected void rectImpl (float x1 , float y1 , float x2 , float y2 ) {
761- // rect.setFrame(x1, y1, x2-x1, y2-y1);
762- // drawShape(rect);
757+ beforeContextDraw ();
763758 if (drawingThinLines ()) {
764759 x1 += 0.5f ;
765760 x2 += 0.5f ;
@@ -785,8 +780,7 @@ protected void rectImpl(float x1, float y1, float x2, float y2) {
785780
786781 @ Override
787782 protected void ellipseImpl (float x , float y , float w , float h ) {
788- // ellipse.setFrame(x, y, w, h);
789- // drawShape(ellipse);
783+ beforeContextDraw ();
790784 if (drawingThinLines ()) {
791785 x += 0.5f ;
792786 y += 0.5f ;
@@ -809,13 +803,15 @@ protected void ellipseImpl(float x, float y, float w, float h) {
809803 @ Override
810804 protected void arcImpl (float x , float y , float w , float h ,
811805 float start , float stop , int mode ) {
812- // 0 to 90 in java would be 0 to -90 for p5 renderer
813- // but that won't work, so -90 to 0?
806+ beforeContextDraw ();
807+
814808 if (drawingThinLines ()) {
815809 x += 0.5f ;
816810 y += 0.5f ;
817811 }
818812
813+ // 0 to 90 in java would be 0 to -90 for p5 renderer
814+ // but that won't work, so -90 to 0?
819815 start = -start ;
820816 stop = -stop ;
821817
@@ -1928,6 +1924,12 @@ protected void fillFromCalc() {
19281924
19291925 @ Override
19301926 public void backgroundImpl () {
1927+
1928+ // if pixels are modified, we don't flush them (just mark them flushed)
1929+ // because they would be immediatelly overwritten by the background anyway
1930+ modified = false ;
1931+ loaded = false ;
1932+
19311933 // This only takes into account cases where this is the primary surface.
19321934 // Not sure what we do with offscreen anyway.
19331935 Paint savedFill = context .getFill ();
@@ -2030,25 +2032,33 @@ public void backgroundImpl() {
20302032
20312033 @ Override
20322034 public void loadPixels () {
2033-
2034- flush ();
2035-
2036- if ((pixels == null ) || (pixels .length != pixelWidth *pixelHeight )) {
2035+ if ((pixels == null ) || (pixels .length != pixelWidth * pixelHeight )) {
20372036 pixels = new int [pixelWidth * pixelHeight ];
2037+ loaded = false ;
20382038 }
20392039
2040- checkSnapshotImage ();
2040+ if (!loaded ) {
2041+ if (snapshotImage == null ||
2042+ snapshotImage .getWidth () != pixelWidth ||
2043+ snapshotImage .getHeight () != pixelHeight ) {
2044+ snapshotImage = new WritableImage (pixelWidth , pixelHeight );
2045+ }
2046+
2047+ SnapshotParameters sp = new SnapshotParameters ();
2048+ if (pixelDensity != 1 ) {
2049+ sp .setTransform (Transform .scale (pixelDensity , pixelDensity ));
2050+ }
2051+ snapshotImage = ((PSurfaceFX ) surface ).canvas .snapshot (sp , snapshotImage );
2052+ PixelReader pr = snapshotImage .getPixelReader ();
2053+ pr .getPixels (0 , 0 , pixelWidth , pixelHeight , argbFormat , pixels , 0 , pixelWidth );
20412054
2042- SnapshotParameters sp = new SnapshotParameters ();
2043- if (pixelDensity == 2 ) {
2044- sp .setTransform (Transform .scale (2 , 2 ));
2055+ loaded = true ;
2056+ modified = false ;
20452057 }
2046- snapshotImage = ((PSurfaceFX ) surface ).canvas .snapshot (sp , snapshotImage );
2047- PixelReader pr = snapshotImage .getPixelReader ();
2048- pr .getPixels (0 , 0 , pixelWidth , pixelHeight , argbFormat , pixels , 0 , pixelWidth );
20492058 }
20502059
20512060
2061+
20522062 //////////////////////////////////////////////////////////////
20532063
20542064 // GET/SET PIXELS
@@ -2083,22 +2093,27 @@ protected void setImpl(PImage sourceImage,
20832093 int sourceX , int sourceY ,
20842094 int sourceWidth , int sourceHeight ,
20852095 int targetX , int targetY ) {
2086-
2087- // Copies the pixels
2088- loadPixels ();
20892096 sourceImage .loadPixels ();
2090- int sourceOffset = sourceY * sourceImage .pixelWidth + sourceX ;
2091- int targetOffset = targetY * pixelWidth + targetX ;
2092- for (int y = sourceY ; y < sourceY + sourceHeight ; y ++) {
2093- System .arraycopy (sourceImage .pixels , sourceOffset , pixels , targetOffset , sourceWidth );
2094- sourceOffset += sourceImage .pixelWidth ;
2095- targetOffset += pixelWidth ;
2096- }
20972097
2098- // Draws the image
2099- copy (sourceImage ,
2100- sourceX , sourceY , sourceWidth , sourceHeight ,
2101- targetX , targetY , sourceWidth , sourceHeight );
2098+ int sourceOffset = sourceX + sourceImage .pixelWidth * sourceY ;
2099+
2100+ PixelWriter pw = context .getPixelWriter ();
2101+ pw .setPixels (targetX , targetY , sourceWidth , sourceHeight ,
2102+ argbFormat ,
2103+ sourceImage .pixels ,
2104+ sourceOffset ,
2105+ sourceImage .pixelWidth );
2106+
2107+ // Let's keep them loaded
2108+ if (loaded ) {
2109+ int sourceStride = sourceImage .pixelWidth ;
2110+ int targetStride = pixelWidth ;
2111+ int targetOffset = targetX + targetY * targetStride ;
2112+ for (int i = 0 ; i < sourceHeight ; i ++) {
2113+ System .arraycopy (sourceImage .pixels , sourceOffset + i * sourceStride ,
2114+ pixels , targetOffset + i * targetStride , sourceWidth );
2115+ }
2116+ }
21022117 }
21032118
21042119
0 commit comments