import com.sun.glass.ui.Robot; import com.sun.javafx.perf.PerformanceTracker; import javafx.application.Application; import javafx.application.Platform; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.stage.Popup; import javafx.stage.Stage; import javafx.stage.WindowEvent; public class HelloPopupPerformance extends Application { // using the VGA size by default static double SCREEN_WIDTH = 640.0; static double SCREEN_HEIGHT = 480.0; static int WARMUP_CYCLES = 5; static int MEASURE_CYCLES = 20; long cycles = WARMUP_CYCLES + MEASURE_CYCLES; long startTime = -1; long summaryLatency = 0; @Override public void start(final Stage stage) { final Popup popup = new Popup(); popup.setAutoHide(true); popup.sizeToScene(); Text text = new Text("Popup performance test"); ImageView imageView = new ImageView("linux_small.png"); VBox vb = new VBox(); vb.getChildren().addAll(text, imageView); vb.setPrefSize(SCREEN_WIDTH, SCREEN_HEIGHT); vb.setAlignment(Pos.CENTER); popup.getContent().add(vb); popup.getScene().setFill(Color.WHITE); stage.setTitle("Hello Popup"); stage.setWidth(SCREEN_WIDTH); stage.setHeight(SCREEN_HEIGHT); Button b = new Button("Open a Popup"); Scene s = new Scene(new Group(b)); s.setFill(Color.LIGHTBLUE); stage.setScene(s); stage.show(); final Robot robot = com.sun.glass.ui.Application.GetApplication().createRobot(); s.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler() { @Override public void handle(MouseEvent event) { popup.show(stage); } }); Runnable roboActions = new Runnable() { @Override public void run() { startTime = System.nanoTime(); robot.mousePress(Robot.MOUSE_LEFT_BTN); robot.mouseRelease(Robot.MOUSE_LEFT_BTN); } }; Runnable popupHide = new Runnable() { @Override public void run() { popup.hide(); } }; // loop controller popup.setOnHidden( new EventHandler() { @Override public void handle(WindowEvent event) { if(--cycles > 0) { // have more cycles to execute if(cycles == MEASURE_CYCLES) { // reset accumulated latencies System.out.println("Warming up completed"); summaryLatency = 0; } Platform.runLater(roboActions); } else { // stop execution System.out.printf("AVERAGE LATENCY = %.2f ms\n",((double)summaryLatency / MEASURE_CYCLES / 1000000)); PerformanceTracker.releaseSceneTracker(popup.getScene()); Platform.exit(); } } }); PerformanceTracker tracker; tracker = PerformanceTracker.getSceneTracker(popup.getScene()); tracker.setOnRenderedFrameTask(new Runnable() { @Override public void run() { if (startTime != -1) { long latency = System.nanoTime() - startTime; summaryLatency += latency; System.out.println("INSTANT LATENCY = " + latency / 1000000 + " ms"); startTime = -1; Platform.runLater(popupHide); } } }); // activity initiator PerformanceTracker.getSceneTracker(s).setOnRenderedFrameTask(new Runnable() { @Override public void run() { PerformanceTracker.releaseSceneTracker(s); Platform.runLater(roboActions); } }); double x = stage.getX() + s.getX() + s.getWidth() / 2; double y = stage.getY() + s.getY() + s.getHeight() / 2; robot.mouseMove((int) x, (int) y); stage.show(); } public static void main(String[] args) { Application.launch(args); } }