FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

deal with some threading issues, remove init() · Jonham/processing@1517ded · GitHub

Repository navigation

Commit 1517ded

Browse files
committed
deal with some threading issues, remove init()
1 parent f1fe23f commit 1517ded

4 files changed

Lines changed: 123 additions & 167 deletions

File tree

‎core/src/processing/core/PApplet.java‎

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.awt.image.BufferedImage;
4141

4242

43+
4344
// used by loadImage() functions
4445
import javax.imageio.ImageIO;
4546
// allows us to remove our own MediaTracker code
@@ -256,7 +257,7 @@ public class PApplet implements PConstants {
256257
* true if no size() command has been executed. This is used to wait until
257258
* a size has been set before placing in the window and showing it.
258259
*/
259-
public boolean defaultSize;
260+
// public boolean defaultSize;
260261

261262
// /** Storage for the current renderer size to avoid re-allocation. */
262263
// Dimension currentSize = new Dimension();
@@ -507,7 +508,7 @@ public class PApplet implements PConstants {
507508
* across platforms and input methods.
508509
*/
509510
@Deprecated
510-
public boolean firstMouse;
511+
public boolean firstMouse = true;
511512

512513
/**
513514
* ( begin auto-generated from mouseButton.xml )
@@ -706,10 +707,10 @@ public class PApplet implements PConstants {
706707
*/
707708
public float frameRate = 10;
708709

709-
protected boolean looping;
710+
protected boolean looping = true;
710711

711712
/** flag set to true when a redraw is asked for by the user */
712-
protected boolean redraw;
713+
protected boolean redraw = true;
713714

714715
/**
715716
* ( begin auto-generated from frameCount.xml )
@@ -828,55 +829,55 @@ public class PApplet implements PConstants {
828829
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
829830

830831

831-
/**
832-
* Applet initialization. This can do GUI work because the components have
833-
* not been 'realized' yet: things aren't visible, displayed, etc.
834-
*/
835-
public void init() {
836-
// println("init() called " + Integer.toHexString(hashCode()));
837-
// using a local version here since the class variable is deprecated
838-
// Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
839-
// screenWidth = screen.width;
840-
// screenHeight = screen.height;
841-
842-
defaultSize = true;
843-
finished = false; // just for clarity
844-
845-
// this will be cleared by draw() if it is not overridden
846-
looping = true;
847-
redraw = true; // draw this guy at least once
848-
firstMouse = true;
849-
850-
// calculated dynamically on first call
851-
// // Removed in 2.1.2, brought back for 2.1.3. Usually sketchPath is set
852-
// // inside runSketch(), but if this sketch takes care of calls to init()
853-
// // when PApplet.main() is not used (i.e. it's in a Java application).
854-
// // THe path needs to be set here so that loadXxxx() functions work.
855-
// if (sketchPath == null) {
856-
// sketchPath = calcSketchPath();
857-
// }
858-
859-
// set during Surface.initFrame()
860-
// // Figure out the available display width and height.
861-
// // No major problem if this fails, we have to try again anyway in
862-
// // handleDraw() on the first (== 0) frame.
863-
// checkDisplaySize();
864-
865-
// // Set the default size, until the user specifies otherwise
866-
// int w = sketchWidth();
867-
// int h = sketchHeight();
868-
// defaultSize = (w == DEFAULT_WIDTH) && (h == DEFAULT_HEIGHT);
832+
// /**
833+
// * Applet initialization. This can do GUI work because the components have
834+
// * not been 'realized' yet: things aren't visible, displayed, etc.
835+
// */
836+
// public void init() {
837+
//// println("init() called " + Integer.toHexString(hashCode()));
838+
// // using a local version here since the class variable is deprecated
839+
//// Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
840+
//// screenWidth = screen.width;
841+
//// screenHeight = screen.height;
869842
//
870-
// g = makeGraphics(w, h, sketchRenderer(), null, true);
871-
// // Fire component resize event
872-
// setSize(w, h);
873-
// setPreferredSize(new Dimension(w, h));
843+
// defaultSize = true;
844+
// finished = false; // just for clarity
874845
//
875-
// width = g.width;
876-
// height = g.height;
877-
878-
surface.startThread();
879-
}
846+
// // this will be cleared by draw() if it is not overridden
847+
// looping = true;
848+
// redraw = true; // draw this guy at least once
849+
// firstMouse = true;
850+
//
851+
// // calculated dynamically on first call
852+
//// // Removed in 2.1.2, brought back for 2.1.3. Usually sketchPath is set
853+
//// // inside runSketch(), but if this sketch takes care of calls to init()
854+
//// // when PApplet.main() is not used (i.e. it's in a Java application).
855+
//// // THe path needs to be set here so that loadXxxx() functions work.
856+
//// if (sketchPath == null) {
857+
//// sketchPath = calcSketchPath();
858+
//// }
859+
//
860+
// // set during Surface.initFrame()
861+
//// // Figure out the available display width and height.
862+
//// // No major problem if this fails, we have to try again anyway in
863+
//// // handleDraw() on the first (== 0) frame.
864+
//// checkDisplaySize();
865+
//
866+
//// // Set the default size, until the user specifies otherwise
867+
//// int w = sketchWidth();
868+
//// int h = sketchHeight();
869+
//// defaultSize = (w == DEFAULT_WIDTH) && (h == DEFAULT_HEIGHT);
870+
////
871+
//// g = makeGraphics(w, h, sketchRenderer(), null, true);
872+
//// // Fire component resize event
873+
//// setSize(w, h);
874+
//// setPreferredSize(new Dimension(w, h));
875+
////
876+
//// width = g.width;
877+
//// height = g.height;
878+
//
879+
// surface.startThread();
880+
// }
880881

881882

882883
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@@ -2368,7 +2369,7 @@ public void handleDraw() {
23682369
// // Give up, instead set the new renderer and re-attempt setup()
23692370
// return;
23702371
// }
2371-
defaultSize = false;
2372+
// defaultSize = false;
23722373

23732374
} else { // frameCount > 0, meaning an actual draw()
23742375
// update the current frameRate
@@ -10119,7 +10120,7 @@ static public void runSketch(final String[] args,
1011910120
sketch.windowColor = windowColor;
1012010121
}
1012110122

10122-
PSurface surface = sketch.initSurface();
10123+
final PSurface surface = sketch.initSurface();
1012310124
// sketch.initSurface(windowColor, displayIndex, fullScreen, spanDisplays);
1012410125

1012510126
/*
@@ -10146,16 +10147,25 @@ static public void runSketch(final String[] args,
1014610147
surface.placeWindow(location, editorLocation);
1014710148
}
1014810149

10150+
// EventQueue.invokeLater(new Runnable() {
10151+
// public void run() {
10152+
1014910153
// Helps avoid this code being duplicated 2x per surface class.
1015010154
// Andres is testing to make sure this doesn't cause trouble.
1015110155
if (sketch.getGraphics().displayable()) {
1015210156
surface.setVisible(true);
1015310157
}
1015410158

1015510159
// not always running externally when in present mode
10156-
if (external) {
10160+
if (sketch.external) {
1015710161
surface.setupExternalMessages();
1015810162
}
10163+
10164+
//sketch.init();
10165+
surface.startThread();
10166+
10167+
// }
10168+
// });
1015910169
}
1016010170

1016110171

@@ -10220,7 +10230,7 @@ private void deprecationWarning(String method) {
1022010230
surface.initOffscreen(this); // for PDF/PSurfaceNone and friends
1022110231
}
1022210232

10223-
init();
10233+
// init();
1022410234
return surface;
1022510235
}
1022610236

‎core/src/processing/core/PSurfaceAWT.java‎

Lines changed: 31 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public class SmoothCanvas extends Canvas {
160160
private Dimension newSize = new Dimension(0, 0);
161161

162162

163+
// Turns out getParent() returns a JPanel on a JFrame. Yech.
163164
public Frame getFrame() {
164165
return frame;
165166
}
@@ -197,7 +198,11 @@ public void validate() {
197198
// System.out.println("validate() render old=" + oldSize + " -> new=" + newSize);
198199
oldSize = newSize;
199200
sketch.setSize(newSize.width, newSize.height);
201+
// try {
200202
render();
203+
// } catch (IllegalStateException ise) {
204+
// System.out.println(ise.getMessage());
205+
// }
201206
}
202207
}
203208

@@ -248,103 +253,35 @@ public void addNotify() {
248253

249254

250255
synchronized protected void render() {
251-
// System.out.println("render() top");
252-
253-
/*
254-
if (!EventQueue.isDispatchThread()) {
255-
//throw new IllegalStateException("render() called outside the EDT");
256-
//System.err.println("render() called outside the EDT");
257-
new Exception("render() called outside the EDT").printStackTrace();
256+
if (canvas.isDisplayable() &&
257+
graphics.image != null) {
258+
if (canvas.getBufferStrategy() == null) {
259+
canvas.createBufferStrategy(2);
258260
}
259-
*/
260-
// if (canvas == null) {
261-
// removeListeners(this);
262-
// canvas = new Canvas();
263-
// add(canvas);
264-
// setIgnoreRepaint(true);
265-
// canvas.setIgnoreRepaint(true);
266-
// addListeners(canvas);
267-
//// add(canvas, BorderLayout.CENTER);
268-
//// doLayout();
269-
// }
270-
271-
// not sure why this was here, can't be good
272-
//canvas.setBounds(0, 0, sketch.width, sketch.height);
273-
274-
if (!canvas.isDisplayable()) {
275-
// System.out.println("no peer.. holding");
276-
return;
277-
}
278-
279-
if (graphics.image == null) {
280-
if (PApplet.DEBUG) {
281-
new Exception("image is null, returning").printStackTrace(System.out);
261+
BufferStrategy strategy = canvas.getBufferStrategy();
262+
if (strategy != null) {
263+
// Render single frame
264+
// try {
265+
do {
266+
// The following loop ensures that the contents of the drawing buffer
267+
// are consistent in case the underlying surface was recreated
268+
do {
269+
Graphics2D draw = (Graphics2D) strategy.getDrawGraphics();
270+
// draw to width/height, since this may be a 2x image
271+
draw.drawImage(graphics.image, 0, 0, sketchWidth, sketchHeight, null);
272+
draw.dispose();
273+
} while (strategy.contentsRestored());
274+
275+
// Display the buffer
276+
strategy.show();
277+
278+
// Repeat the rendering if the drawing buffer was lost
279+
} while (strategy.contentsLost());
282280
}
283-
return;
284281
}
285-
286-
Canvas c = canvas;
287-
// Frame c = frame;
288-
// System.out.println("render(), canvas bounds are " + canvas.getBounds());
289-
if (c.getBufferStrategy() == null) { // whole block [121222]
290-
// System.out.println("creating a strategy");
291-
c.createBufferStrategy(2);
292-
}
293-
BufferStrategy strategy = c.getBufferStrategy();
294-
// System.out.println(strategy);
295-
if (strategy == null) {
296-
return;
297-
}
298-
// Render single frame
299-
do {
300-
// The following loop ensures that the contents of the drawing buffer
301-
// are consistent in case the underlying surface was recreated
302-
do {
303-
Graphics2D draw = (Graphics2D) strategy.getDrawGraphics();
304-
//draw.drawImage(pg.image, 0, 0, sketch.width, sketch.height, null);
305-
//System.out.println("render() drawing image");
306-
/*
307-
while (sketch.insideDraw) {
308-
System.out.println("render() yielding because inside draw");
309-
//Thread.yield();
310-
try {
311-
Thread.sleep(1);
312-
} catch (InterruptedException e) { }
313-
}
314-
*/
315-
316-
// this wasn't any faster than setting the image size while drawing
317-
// if (graphics.pixelFactor == 2) {
318-
// draw.scale(0.5, 0.5);
319-
// }
320-
321-
// draw to width/height, since this may be a 2x image
322-
draw.drawImage(graphics.image, 0, 0, sketchWidth, sketchHeight, null);
323-
// draw.drawImage(graphics.image, 0, 0, null);
324-
draw.dispose();
325-
326-
// Repeat the rendering if the drawing buffer contents
327-
// were restored
328-
// System.out.println("restored " + strategy.contentsRestored());
329-
} while (strategy.contentsRestored());
330-
331-
// Display the buffer
332-
// System.out.println("showing");
333-
strategy.show();
334-
335-
// Repeat the rendering if the drawing buffer was lost
336-
// System.out.println("lost " + strategy.contentsLost());
337-
// System.out.println();
338-
} while (strategy.contentsLost());
339-
// System.out.println("render() bottom");
340282
}
341283

342284

343-
// Object offscreenLock = new Object();
344-
// BufferedImage offscreen;
345-
// BufferedImage onscreen;
346-
347-
348285
/*
349286
protected void blit() {
350287
// Other folks that call render() (i.e. paint()) are already on the EDT.
@@ -762,8 +699,10 @@ private void setFullFrame() {
762699
//frame.setExtendedState(Frame.MAXIMIZED_BOTH);
763700

764701
// https://github.com/processing/processing/pull/3162
765-
frame.dispose(); // release native resources, allows setUndecorated()
702+
//frame.dispose(); // release native resources, allows setUndecorated()
703+
frame.removeNotify();
766704
frame.setUndecorated(true);
705+
frame.addNotify();
767706

768707
// this may be the bounds of all screens
769708
frame.setBounds(screenRect);
@@ -1479,7 +1418,6 @@ public Thread createThread() {
14791418
@Override
14801419
public void callDraw() {
14811420
sketch.handleDraw();
1482-
//blit();
14831421
render();
14841422
}
14851423
};

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL