Skip to content

Commit bf40b12

Browse files
author
Pavel Marek
committed
[GR-30199] Make JavaGD based graphics the default.
PullRequest: fastr/2647
2 parents 735617b + e8df760 commit bf40b12

File tree

46 files changed

+895
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+895
-132
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,4 @@ lib.install.packages.gnur
164164
com.oracle.truffle.r.test.native/packages/*/*/src/*.so
165165
com.oracle.truffle.r.test.native/packages/*/*/src/*.o
166166
tags
167+
com.oracle.truffle.r.test.packages/r/fastr.functions.rdx

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# 22.1.0
22
* Improved performance of the `order` and `rank` builtin functions
3+
* Use JavaGD as the default graphical subsystem.
4+
* Deprecate `--R.UseInternalGridGraphics` option.
5+
* The FastR's graphical subsystem is now mostly compatible with GNU-R's, i.e., most functions from `graphics`, `grid`, and `grDevices` base packages are now supported.
6+
* Display lists are fully implemented.
7+
* Supported devices: SVG, PNG, JPEG, BMP, AWT.
8+
* See [graphics docs](./documentation/dev/graphics.md).
39

410
# 22.0.0
511
* Adopted [NodeLibrary](https://www.graalvm.org/truffle/javadoc/com/oracle/truffle/api/interop/NodeLibrary.html).

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2901,8 +2901,9 @@ private static JavaGDContext getJavaGDContext(RContext ctx) {
29012901

29022902
@Override
29032903
@TruffleBoundary
2904-
public void gdOpen(int gdId, String deviceName, double w, double h, RContext ctx) {
2905-
JavaGDContext.getContext(ctx).newGD(gdId, deviceName).gdOpen(w, h);
2904+
public boolean gdOpen(int gdId, String deviceName, double w, double h, RContext ctx) {
2905+
boolean result = JavaGDContext.getContext(ctx).newGD(gdId, deviceName).gdOpen(w, h);
2906+
return result;
29062907
}
29072908

29082909
@Override
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 3 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 3 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 3 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package com.oracle.truffle.r.ffi.impl.javaGD;
25+
26+
import org.rosuda.javaGD.GDContainer;
27+
import org.rosuda.javaGD.GDObject;
28+
import org.rosuda.javaGD.GDState;
29+
import org.rosuda.javaGD.LocatorSync;
30+
31+
import java.awt.*;
32+
import java.util.Collection;
33+
import java.util.LinkedList;
34+
import java.util.List;
35+
36+
/**
37+
* Implementation of {@link GDContainer} that wraps {@link Graphics}.
38+
*/
39+
public class AWTGraphicsContainer implements GDContainer {
40+
private final GDState gdState;
41+
private final List<GDObject> objects;
42+
private final Dimension size;
43+
private Graphics graphics;
44+
private int deviceNumber;
45+
46+
public AWTGraphicsContainer(double width, double height) {
47+
this.gdState = new GDState();
48+
this.objects = new LinkedList<>();
49+
this.size = new Dimension((int) width, (int) height);
50+
this.deviceNumber = -1;
51+
}
52+
53+
public void setGraphics(Graphics graphics) {
54+
assert this.graphics == null : "graphics field should be set just once";
55+
assert this.objects.size() == 0;
56+
this.graphics = graphics;
57+
// clearRect is necessary so that objects created with, e.g., `grid.rect` are visible
58+
// on the graphics object.
59+
this.graphics.clearRect(0, 0, size.width, size.height);
60+
}
61+
62+
@Override
63+
public void add(GDObject o) {
64+
objects.add(o);
65+
o.paint(null, gdState, graphics);
66+
}
67+
68+
@Override
69+
public Collection<GDObject> getGDObjects() {
70+
return objects;
71+
}
72+
73+
@Override
74+
public void reset(int pageNumber) {
75+
objects.clear();
76+
graphics.clearRect(0, 0, size.width, size.height);
77+
}
78+
79+
@Override
80+
public GDState getGState() {
81+
return gdState;
82+
}
83+
84+
@Override
85+
public Graphics getGraphics() {
86+
return graphics;
87+
}
88+
89+
@Override
90+
public boolean prepareLocator(LocatorSync ls) {
91+
return false;
92+
}
93+
94+
@Override
95+
public void syncDisplay(boolean finish) {
96+
// nop
97+
}
98+
99+
@Override
100+
public void setDeviceNumber(int dn) {
101+
this.deviceNumber = dn;
102+
}
103+
104+
@Override
105+
public void closeDisplay() {
106+
objects.clear();
107+
}
108+
109+
@Override
110+
public int getDeviceNumber() {
111+
return deviceNumber;
112+
}
113+
114+
@Override
115+
public Dimension getSize() {
116+
return size;
117+
}
118+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 3 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 3 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 3 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package com.oracle.truffle.r.ffi.impl.javaGD;
25+
26+
import org.rosuda.javaGD.GDInterface;
27+
28+
import java.awt.*;
29+
30+
public class AWTGraphicsGD extends GDInterface {
31+
private Graphics graphics;
32+
33+
public void setGraphics(Graphics graphics) {
34+
assert this.graphics == null : "graphics field should be initialized just once";
35+
this.graphics = graphics;
36+
((AWTGraphicsContainer) c).setGraphics(graphics);
37+
}
38+
39+
@Override
40+
public boolean gdOpen(double w, double h) {
41+
c = new AWTGraphicsContainer(w, h);
42+
return true;
43+
}
44+
}

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/javaGD/AbstractImageContainer.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
2929
import java.awt.Graphics2D;
3030
import java.awt.RenderingHints;
3131
import java.io.IOException;
32+
import java.util.Collection;
3233
import java.util.LinkedList;
3334
import java.util.List;
3435

@@ -59,6 +60,11 @@ public void add(GDObject o) {
5960
objects.add(o);
6061
}
6162

63+
@Override
64+
public Collection<GDObject> getGDObjects() {
65+
return objects;
66+
}
67+
6268
protected abstract void resetGraphics();
6369

6470
@Override
@@ -78,7 +84,7 @@ public boolean prepareLocator(LocatorSync ls) {
7884
}
7985

8086
@TruffleBoundary
81-
protected void repaint() {
87+
public void repaint() {
8288
Graphics graphics = getGraphics();
8389
graphics.clearRect(0, 0, size.width, size.height);
8490
for (GDObject o : objects) {
@@ -87,7 +93,7 @@ protected void repaint() {
8793
}
8894

8995
@Override
90-
protected final void saveImage(TruffleFile file) throws IOException {
96+
public final void saveImage(TruffleFile file) throws IOException {
9197
repaint();
9298
dumpImage(file);
9399
}

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/javaGD/BufferedImageContainer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,7 +47,7 @@ public class BufferedImageContainer extends AbstractImageContainer {
4747
private final String fileType;
4848
private final float quality;
4949

50-
BufferedImageContainer(int width, int height, String fileType, String fileNameTemplate, float quality) {
50+
public BufferedImageContainer(int width, int height, String fileType, String fileNameTemplate, float quality) {
5151
super(width, height, fileNameTemplate);
5252

5353
if (!isSupportedFormat(fileType)) {

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/javaGD/BufferedImageGD.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -43,9 +43,10 @@ public BufferedImageGD(String fileType, String fileNameTemplate, String params)
4343
}
4444

4545
@Override
46-
public void gdOpen(double width, double height) {
46+
public boolean gdOpen(double width, double height) {
4747
super.gdOpen(width, height);
4848
c = new BufferedImageContainer((int) width, (int) height, fileType, fileNameTemplate, quality);
49+
return true;
4950
}
5051

5152
}

com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/javaGD/FileOutputContainer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,7 +32,7 @@
3232

3333
public abstract class FileOutputContainer implements GDContainer {
3434

35-
protected final String fileNameTemplate;
35+
protected String fileNameTemplate;
3636

3737
private int pageNumber = 0;
3838

@@ -44,6 +44,14 @@ protected int getPageNumber() {
4444
return pageNumber;
4545
}
4646

47+
public String getFileNameTemplate() {
48+
return fileNameTemplate;
49+
}
50+
51+
public void setFileNameTemplate(String fileNameTemplate) {
52+
this.fileNameTemplate = fileNameTemplate;
53+
}
54+
4755
@Override
4856
public void reset(int explicitPageNumber) {
4957
saveImage();

0 commit comments

Comments
 (0)