Skip to content

Commit ce575e9

Browse files
committed
Reflective access to GraphicsConfiguration for FBO layer control. Issue #4104.
1 parent 61d1a9a commit ce575e9

2 files changed

Lines changed: 38 additions & 11 deletions

File tree

core/src/processing/javafx/PSurfaceFX.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,11 @@ public boolean isStopped() {
730730
return animation.getStatus() == Animation.Status.STOPPED;
731731
}
732732

733+
@Override
734+
public boolean isFboAllowed() {
735+
return true;
736+
}
737+
733738

734739
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
735740

core/src/processing/opengl/PSurfaceJOGL.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.FileInputStream;
3737
import java.io.IOException;
3838
import java.io.InputStream;
39+
import java.lang.reflect.Method;
3940
import java.nio.ByteBuffer;
4041
import java.util.HashMap;
4142
import java.util.Map;
@@ -74,8 +75,6 @@
7475
import processing.core.PSurface;
7576
import processing.event.KeyEvent;
7677
import processing.event.MouseEvent;
77-
import sun.java2d.opengl.CGLGraphicsConfig;
78-
import sun.java2d.pipe.hw.ContextCapabilities;
7978

8079

8180
public class PSurfaceJOGL implements PSurface {
@@ -126,7 +125,7 @@ public class PSurfaceJOGL implements PSurface {
126125
* Contains information about the environments on which FBO should NOT be used.
127126
* Each position of the array contains a pair of {@link String}s correspondent to
128127
* the operating system (based on {@link PConstants}) and the name of the
129-
* hardware (based on {@link ContextCapabilities#adapterId}), respectively.
128+
* hardware (based on sun.java2d.pipe.hw.ContextCapabilities#adapterId), respectively.
130129
*/
131130
final protected String[][] antiFboEnvironments = new String[][]{
132131
{String.valueOf(PConstants.MACOSX), "Intel HD Graphics 3000"}
@@ -208,20 +207,43 @@ protected void initDisplay() {
208207
* If in future this resource must be used by other platforms, the main 'if' clause
209208
* should be removed, allowing direct access to its inner logic.
210209
*/
211-
private void initFboControl(GraphicsConfiguration config){
212-
if(PApplet.platform == PApplet.MACOSX){
213-
for(String[] pair : antiFboEnvironments){
214-
if(pair[0].equals(String.valueOf(PApplet.platform))){
215-
String adapterId = ((CGLGraphicsConfig) config).getContextCapabilities().getAdapterId();
216-
if(adapterId.toLowerCase().contains(pair[1].toLowerCase())){
217-
this.fboAllowed = false;
218-
break;
210+
private void initFboControl(GraphicsConfiguration config) {
211+
if (PApplet.platform == PApplet.MACOSX) {
212+
try {
213+
String adapterId = getAdapterId(config);
214+
for (String[] pair : antiFboEnvironments) {
215+
if (pair[0].equals(String.valueOf(PApplet.platform))) {
216+
if (adapterId.toLowerCase().contains(pair[1].toLowerCase())) {
217+
this.fboAllowed = false;
218+
break;
219+
}
219220
}
220221
}
222+
} catch (Exception e) {
223+
e.printStackTrace();
221224
}
222225
}
223226
}
224227

228+
/**
229+
* Evaluates the graphics configuration through reflection to safely get the adapter
230+
* id from the hardware detected.
231+
*
232+
* @param config graphics configuration
233+
* @return the name of the adapter id as a String
234+
* @throws Exception when there is a problem with reflection
235+
*/
236+
private String getAdapterId(GraphicsConfiguration config) throws Exception {
237+
Class cglClass = Class.forName("sun.java2d.opengl.CGLGraphicsConfig");
238+
Class ctcClass = Class.forName("sun.java2d.pipe.hw.ContextCapabilities");
239+
Method cglMethod = cglClass.getMethod("getContextCapabilities");
240+
Method ctcMethod = ctcClass.getMethod("getAdapterId");
241+
Object cglInstance = cglClass.cast(config);
242+
Object ctcInstance = cglMethod.invoke(cglInstance);
243+
Object idInstance = ctcMethod.invoke(ctcInstance);
244+
return String.valueOf(idInstance);
245+
}
246+
225247
@Override
226248
public boolean isFboAllowed(){
227249
return this.fboAllowed;

0 commit comments

Comments
 (0)