Skip to content

Commit

Permalink
Implemented grid-device server.
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-s authored and Miloslav Metelka committed Aug 27, 2018
1 parent 2a80638 commit 35057c5
Show file tree
Hide file tree
Showing 12 changed files with 1,587 additions and 39 deletions.

Large diffs are not rendered by default.

Expand Up @@ -33,7 +33,8 @@
import com.oracle.truffle.r.library.fastrGrid.device.GridDevice.DeviceCloseException;
import com.oracle.truffle.r.library.fastrGrid.device.SVGDevice;
import com.oracle.truffle.r.library.fastrGrid.device.awt.BufferedImageDevice;
import com.oracle.truffle.r.library.fastrGrid.device.awt.BufferedImageDevice.NotSupportedImageFormatException;
import com.oracle.truffle.r.library.fastrGrid.device.NotSupportedImageFormatException;
import com.oracle.truffle.r.library.fastrGrid.device.remote.RemoteDevice;
import com.oracle.truffle.r.library.fastrGrid.grDevices.FileDevUtils;
import com.oracle.truffle.r.library.fastrGrid.graphics.RGridGraphicsAdapter;
import com.oracle.truffle.r.runtime.FastRConfig;
Expand Down Expand Up @@ -106,6 +107,14 @@ public static GridContext getContext() {
return getContext(RContext.getInstance());
}

public static GridDevice openLocalOrRemoteDevice(String filename, String fileType, int width, int height) throws NotSupportedImageFormatException {
if (FastRConfig.UseRemoteGridAwtDevice) {
return RemoteDevice.open(filename, fileType, width, height);
} else {
return BufferedImageDevice.open(filename, fileType, width, height);
}
}

@TruffleBoundary
public GridState getGridState() {
gridState.setDeviceState(devices.get(currentDeviceIdx).state);
Expand Down Expand Up @@ -153,7 +162,7 @@ public void openDefaultDevice() {
if (!FastRConfig.InternalGridAwtSupport) {
throw awtNotSupported();
}
setCurrentDevice(defaultDev, WindowDevice.createWindowDevice());
setCurrentDevice(defaultDev, WindowDevice.createWindowDevice(false, GridDevice.DEFAULT_WIDTH, GridDevice.DEFAULT_HEIGHT));
} else if (defaultDev.equals("svg")) {
String filename = "Rplot%03d.svg";
SVGDevice svgDevice = new SVGDevice(FileDevUtils.formatInitialFilename(filename), GridDevice.DEFAULT_WIDTH, GridDevice.DEFAULT_HEIGHT);
Expand Down Expand Up @@ -200,12 +209,9 @@ public Object evalInternalRFunction(String functionName, Object... args) {
}

private void safeOpenImageDev(String filename, String formatName) {
if (!FastRConfig.InternalGridAwtSupport) {
throw awtNotSupported();
}
BufferedImageDevice dev = null;
GridDevice dev = null;
try {
dev = BufferedImageDevice.open(FileDevUtils.formatInitialFilename(filename), formatName, GridDevice.DEFAULT_WIDTH, GridDevice.DEFAULT_HEIGHT);
dev = openLocalOrRemoteDevice(FileDevUtils.formatInitialFilename(filename), formatName, GridDevice.DEFAULT_WIDTH, GridDevice.DEFAULT_HEIGHT);
} catch (NotSupportedImageFormatException e) {
throw RInternalError.shouldNotReachHere("Device format " + formatName + " should be supported.");
}
Expand Down
Expand Up @@ -24,7 +24,9 @@

import com.oracle.truffle.r.library.fastrGrid.device.GridDevice;
import com.oracle.truffle.r.library.fastrGrid.device.awt.JFrameDevice;
import com.oracle.truffle.r.library.fastrGrid.device.remote.RemoteDevice;
import com.oracle.truffle.r.nodes.function.PromiseHelperNode;
import com.oracle.truffle.r.runtime.FastRConfig;
import com.oracle.truffle.r.runtime.RCaller;
import com.oracle.truffle.r.runtime.RError;
import com.oracle.truffle.r.runtime.RError.Message;
Expand All @@ -41,20 +43,23 @@ private WindowDevice() {
// only static members
}

public static GridDevice createWindowDevice() {
return createWindowDevice(GridDevice.DEFAULT_WIDTH, GridDevice.DEFAULT_HEIGHT);
}

public static GridDevice createWindowDevice(int width, int height) {
JFrameDevice frameDevice = new JFrameDevice(width, height);
RContext ctx = RContext.getInstance();
if (ctx.hasExecutor()) {
frameDevice.setResizeListener(() -> redrawAll(ctx));
frameDevice.setCloseListener(() -> devOff(ctx));
} else {
public static GridDevice createWindowDevice(boolean byGridServer, int width, int height) {
if (FastRConfig.UseRemoteGridAwtDevice) {
noSchedulingSupportWarning();
return RemoteDevice.createWindowDevice(width, height);
} else {
JFrameDevice frameDevice = new JFrameDevice(width, height);
RContext ctx;
if (!byGridServer && ((ctx = RContext.getInstance()) != null) && ctx.hasExecutor() && !FastRConfig.UseRemoteGridAwtDevice) {
frameDevice.setResizeListener(() -> redrawAll(ctx));
frameDevice.setCloseListener(() -> devOff(ctx));
} else {
if (!byGridServer) {
noSchedulingSupportWarning();
}
}
return frameDevice;
}
return frameDevice;
}

public static RError awtNotSupported() {
Expand Down Expand Up @@ -103,7 +108,8 @@ private static RFunction getDevOffFunction(RContext ctx) {
}

private static void noSchedulingSupportWarning() {
// Note: the PolyglotEngine was not built with an Executor
RError.warning(RError.NO_CALLER, Message.GENERIC, "Grid cannot resize the drawings. If you resize the window, the content will be lost.");
// Note: the PolyglotEngine was not built with an Executor or we use remote grid device
RError.warning(RError.NO_CALLER, Message.GENERIC, "Grid cannot resize the drawings. If you resize the window, the content will be lost. " +
"You can redraw the contents using: 'popViewport(0, recording = FALSE); grid:::draw.all()'.");
}
}
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 3 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 3 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.r.library.fastrGrid.device;

public class NotSupportedImageFormatException extends Exception {

private static final long serialVersionUID = 1182697755931636217L;

@Override
public synchronized Throwable fillInStackTrace() {
return this;
}

}
Expand Up @@ -22,6 +22,7 @@
*/
package com.oracle.truffle.r.library.fastrGrid.device.awt;

import com.oracle.truffle.r.library.fastrGrid.device.NotSupportedImageFormatException;
import static java.awt.image.BufferedImage.TYPE_INT_RGB;

import java.awt.Color;
Expand Down Expand Up @@ -102,12 +103,4 @@ private static boolean isSupportedFormat(String formatName) {
return false;
}

public static class NotSupportedImageFormatException extends Exception {
private static final long serialVersionUID = 1182697755931636217L;

@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
}
}

0 comments on commit 35057c5

Please sign in to comment.