001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Oleg V. Khaschansky
019: * @version $Revision$
020: *
021: * @date: Nov 15, 2005
022: */package org.apache.harmony.awt.gl.linux;
023:
024: import java.awt.*;
025: import java.lang.reflect.InvocationTargetException;
026:
027: import org.apache.harmony.awt.gl.GLGraphicsDevice;
028: import org.apache.harmony.awt.gl.Utils;
029: import org.apache.harmony.awt.nativebridge.Int32Pointer;
030: import org.apache.harmony.awt.nativebridge.NativeBridge;
031: import org.apache.harmony.awt.nativebridge.linux.X11;
032: import org.apache.harmony.awt.nativebridge.linux.X11Defs;
033: import org.apache.harmony.awt.ContextStorage;
034:
035: public class XGraphicsDevice extends GLGraphicsDevice {
036: private static final X11 x11 = X11.getInstance();
037:
038: long display;
039: int screen;
040:
041: //private X11.Display xdisplay = null;
042: private X11.Screen xscreen = null;
043:
044: private XGraphicsConfiguration configs[] = null;
045: private int defaultConfigIdx;
046:
047: private DisplayMode displayMode;
048:
049: XGraphicsDevice(long display, int screen) {
050: this .display = display;
051: this .screen = screen;
052:
053: displayMode = new DisplayMode(getDisplayWidth(),
054: getDisplayHeight(), DisplayMode.BIT_DEPTH_MULTI,
055: DisplayMode.REFRESH_RATE_UNKNOWN);
056: }
057:
058: private final X11.Screen getXscreen() {
059: if (xscreen == null)
060: xscreen = x11.createScreen(x11.XScreenOfDisplay(display,
061: screen));
062:
063: return xscreen;
064: }
065:
066: private final XGraphicsConfiguration[] getConfigs() {
067: if (configs == null) {
068: //createConfigs();
069:
070: if (true || EventQueue.isDispatchThread()) {
071: createConfigs();
072: } else {
073: boolean wasAWTLocked = false;
074: try {
075: // XXX - todo - This is a hack actually, should be discussed further
076: try {
077: ContextStorage.getSynchronizer()
078: .storeStateAndFree();
079: wasAWTLocked = true;
080: } catch (RuntimeException e) {
081: }
082:
083: try {
084: EventQueue.invokeAndWait(new Runnable() {
085: public void run() {
086: createConfigs();
087: }
088: });
089: } catch (InterruptedException e) {
090: e.printStackTrace();
091: } catch (InvocationTargetException e) {
092: e.printStackTrace();
093: }
094: } finally {
095: if (wasAWTLocked)
096: ContextStorage.getSynchronizer()
097: .lockAndRestoreState();
098: }
099: }
100: }
101:
102: return configs;
103: }
104:
105: void createConfigs() {
106: // First get default visual ID
107: long defVisualPtr = x11.XDefaultVisual(display, screen);
108: long defVisId = x11.XVisualIDFromVisual(defVisualPtr);
109:
110: // Allocate one int to get number of visual infos by ref
111: Int32Pointer numVisualInfosPtr = NativeBridge.getInstance()
112: .createInt32Pointer(1, true);
113:
114: // Create template visual to obtain visuals for current screen only
115: X11.XVisualInfo vinfo_template = x11.createXVisualInfo(true);
116: vinfo_template.set_screen(screen);
117:
118: // Obtain infos
119: X11.XVisualInfo infosPtr = x11.XGetVisualInfo(display,
120: X11Defs.VisualScreenMask, vinfo_template,
121: numVisualInfosPtr);
122: vinfo_template.free(); // Free template data
123:
124: int numVisualInfos = numVisualInfosPtr.get(0);
125: numVisualInfosPtr.free();
126:
127: // Allocate array for configurations
128: configs = new XGraphicsConfiguration[numVisualInfos];
129:
130: String opengl = System.getProperty("java2d.opengl"); //$NON-NLS-1$
131: boolean useOpenGL = opengl != null && opengl.equals("true"); //$NON-NLS-1$
132:
133: for (int i = 0; i < numVisualInfos; i++) {
134: X11.XVisualInfo info = x11.createXVisualInfo(infosPtr
135: .getElementPointer(i * infosPtr.size()));
136: configs[i] = useOpenGL ? new GLXGraphicsConfiguration(this ,
137: info) : new XGraphicsConfiguration(this , info);
138:
139: if (info.get_visualid() == defVisId)
140: defaultConfigIdx = i;
141: }
142:
143: if (useOpenGL) {
144: //defVisId = 36;
145: defVisId = GLXGraphicsConfiguration.GlxConfigsRec
146: .getBestGLXVisualId(display, screen);
147: for (int i = 0; i < numVisualInfos; i++) {
148: if (configs[i].info.get_visualid() == defVisId) {
149: defaultConfigIdx = i;
150: break;
151: }
152: }
153: }
154: }
155:
156: public int getType() {
157: return TYPE_RASTER_SCREEN;
158: }
159:
160: public GraphicsConfiguration getDefaultConfiguration() {
161: return getConfigs()[defaultConfigIdx];
162: }
163:
164: public GraphicsConfiguration[] getConfigurations() {
165: return getConfigs();
166: }
167:
168: public String getIDstring() {
169: long strPtr = x11.XDisplayString(display);
170: return Utils.straccess.createStringUTF(strPtr);
171: }
172:
173: int getDisplayWidth() {
174: return getXscreen().get_width();
175: }
176:
177: int getDisplayHeight() {
178: return getXscreen().get_height();
179: }
180:
181: protected void finalize() {
182: // Here we have to XFree all XVisualInfo's allocated by createConfigs
183: // First info contains a pointer to memory allocated by XGetVisualInfo
184: if (configs != null && configs.length != 0) {
185: x11.XFree(configs[0].info);
186: }
187: }
188:
189: public int getScreen() {
190: return screen;
191: }
192:
193: public DisplayMode getDisplayMode() {
194: return displayMode;
195: }
196:
197: public DisplayMode[] getDisplayModes() {
198: DisplayMode[] dms = { displayMode };
199: return dms;
200: }
201:
202: public Dimension getResolution() {
203: return new Dimension(
204: (int) (x11.XDisplayWidth(display, screen) * 25.4 / x11
205: .XDisplayWidthMM(display, screen)), (int) (x11
206: .XDisplayHeight(display, screen) * 25.4 / x11
207: .XDisplayHeightMM(display, screen)));
208: }
209: }
|