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 Alexey A. Petrenko
019: * @version $Revision$
020: */package org.apache.harmony.awt.gl.windows;
021:
022: import java.awt.Dimension;
023: import java.awt.DisplayMode;
024: import java.awt.GraphicsConfiguration;
025: import java.awt.Rectangle;
026: import java.util.Vector;
027:
028: import org.apache.harmony.awt.gl.GLGraphicsDevice;
029: import org.apache.harmony.awt.internal.nls.Messages;
030: import org.apache.harmony.awt.nativebridge.windows.Win32;
031: import org.apache.harmony.awt.nativebridge.windows.WindowsDefs;
032:
033: /**
034: * Windows GraphicsDevice implementation
035: *
036: */
037: public class WinGraphicsDevice extends GLGraphicsDevice {
038: private DisplayMode displayMode = null;
039:
040: private static final Win32 win32 = Win32.getInstance();
041:
042: private final int type;
043: private final Rectangle bounds;
044: private final String id;
045: private final boolean primary;
046:
047: private Dimension resolution = null;
048:
049: private WinGraphicsConfiguration defaultConfig = null;
050: private WinGraphicsConfiguration[] configs = null;
051:
052: private byte[] idBytes = null;
053:
054: static boolean useOpenGL;
055: static boolean useGDI;
056: static {
057: String opengl = System.getProperty("java2d.opengl"); //$NON-NLS-1$
058: String gdi = System.getProperty("java2d.gdi"); //$NON-NLS-1$
059: useOpenGL = opengl != null && opengl.equals("true"); //$NON-NLS-1$
060: useGDI = gdi != null && gdi.equals("true"); //$NON-NLS-1$
061: };
062:
063: public WinGraphicsDevice(int left, int top, int right, int bottom,
064: String id, boolean primary) {
065: type = TYPE_RASTER_SCREEN;
066: bounds = new Rectangle(left, top, right - left, bottom - top);
067: displayMode = new DisplayMode(bounds.width, bounds.height,
068: DisplayMode.BIT_DEPTH_MULTI,
069: DisplayMode.REFRESH_RATE_UNKNOWN);
070: this .id = id;
071: this .primary = primary;
072: }
073:
074: public WinGraphicsDevice(long hwnd) {
075: type = TYPE_RASTER_SCREEN;
076:
077: long hmon = win32.MonitorFromWindow(hwnd,
078: WindowsDefs.MONITOR_DEFAULTTOPRIMARY);
079: Win32.MONITORINFOEXW mi = win32.createMONITORINFOEXW(true);
080: mi.get_MONITORINFO().set_cbSize(mi.size());
081: if (win32.GetMonitorInfoW(hmon, mi.shortLockPointer()) == 0) {
082: // awt.15=Can not get monitor info
083: throw new RuntimeException(Messages.getString("awt.15")); //$NON-NLS-1$
084: }
085: Win32.RECT rect = mi.get_MONITORINFO().get_rcMonitor();
086: int x = rect.get_left();
087: int y = rect.get_top();
088: int width = rect.get_right() - x;
089: int height = rect.get_bottom() - y;
090: bounds = new Rectangle(x, y, width, height);
091: displayMode = new DisplayMode(bounds.width, bounds.height,
092: DisplayMode.BIT_DEPTH_MULTI,
093: DisplayMode.REFRESH_RATE_UNKNOWN);
094: id = mi.get_szDevice().getString();
095: primary = (mi.get_MONITORINFO().get_dwFlags() & WindowsDefs.MONITORINFOF_PRIMARY) == WindowsDefs.MONITORINFOF_PRIMARY;
096: rect.free();
097: mi.free();
098: }
099:
100: @Override
101: public int getType() {
102: return type;
103: }
104:
105: @Override
106: public GraphicsConfiguration getDefaultConfiguration() {
107: if (defaultConfig == null) {
108: long hdc = win32.CreateDCW(null, id, null, null);
109: if (hdc == 0) {
110: // awt.16=Can not create DC for device
111: throw new RuntimeException(Messages.getString("awt.16")); //$NON-NLS-1$
112: }
113:
114: int dci = win32.GetPixelFormat(hdc);
115: Win32.PIXELFORMATDESCRIPTOR pfd = win32
116: .createPIXELFORMATDESCRIPTOR(false);
117: if (win32.DescribePixelFormat(hdc, dci, pfd.size(), pfd) > 0) {
118: defaultConfig = useOpenGL ? new WGLGraphicsConfiguration(
119: this , dci, pfd)
120: : new WinGraphicsConfiguration(this , dci, pfd);
121: } else {
122: getConfigurations();
123: }
124: win32.DeleteDC(hdc);
125: }
126:
127: return defaultConfig;
128: }
129:
130: @Override
131: public GraphicsConfiguration[] getConfigurations() {
132: if (configs == null) {
133: long hdc = win32.CreateDCW(null, id, null, null);
134: if (hdc == 0) {
135: // awt.16=Can not create DC for device
136: throw new RuntimeException(Messages.getString("awt.16")); //$NON-NLS-1$
137: }
138:
139: // If we created DC why do not ask it about resolution?
140: if (resolution == null) {
141: int width = win32.GetDeviceCaps(hdc,
142: WindowsDefs.LOGPIXELSY);
143: int height = win32.GetDeviceCaps(hdc,
144: WindowsDefs.LOGPIXELSX);
145: resolution = new Dimension(width, height);
146: }
147:
148: Win32.PIXELFORMATDESCRIPTOR pfd = win32
149: .createPIXELFORMATDESCRIPTOR(false);
150: int pfnum = win32.DescribePixelFormat(hdc, 1, pfd.size(),
151: pfd);
152: if (pfnum == 0) {
153: return null;
154: }
155:
156: // Choose default configuration
157: int dci = win32.GetPixelFormat(hdc);
158:
159: Vector<WinGraphicsConfiguration> gcv = new Vector<WinGraphicsConfiguration>(
160: 100);
161: int i = 1;
162: while (win32.DescribePixelFormat(hdc, i, pfd.size(), pfd) > 0) {
163: WinGraphicsConfiguration gc = useOpenGL ? new WGLGraphicsConfiguration(
164: this , i, pfd)
165: : new WinGraphicsConfiguration(this , i, pfd);
166:
167: if (!gcv.contains(gc)) {
168: gcv.add(gc);
169: }
170:
171: // Is default PixelFormat?
172: if (dci == i) {
173: int idx = gcv.indexOf(gc);
174: gcv.set(idx, gc);
175: defaultConfig = gc;
176: }
177:
178: i++;
179: }
180: pfd.free();
181:
182: win32.DeleteDC(hdc);
183:
184: configs = new WinGraphicsConfiguration[gcv.size()];
185: gcv.toArray(configs);
186:
187: // If we can not find default PixelFormat
188: // let's use first one
189: // Probably not best idea...
190: if (defaultConfig == null) {
191: defaultConfig = configs[0];
192: }
193: }
194:
195: return configs;
196: }
197:
198: @Override
199: public String getIDstring() {
200: return id;
201: }
202:
203: public boolean isDefaultDevice() {
204: return primary;
205: }
206:
207: public Rectangle getBounds() {
208: return bounds;
209: }
210:
211: @Override
212: public String toString() {
213: return getClass().getName()
214: + "[Bounds: " + bounds + ", ID: " + id + ", primary: " + primary + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
215: }
216:
217: byte[] getIDBytes() {
218: if (idBytes == null) {
219: idBytes = (id + "\0x00").getBytes(); //$NON-NLS-1$
220: }
221:
222: return idBytes;
223: }
224:
225: @Override
226: public DisplayMode getDisplayMode() {
227: return displayMode;
228: }
229:
230: @Override
231: public DisplayMode[] getDisplayModes() {
232: DisplayMode[] dms = { displayMode };
233: return dms;
234: }
235:
236: @Override
237: public Dimension getResolution() {
238: if (resolution == null) {
239: long hdc = win32.CreateDCW(null, id, null, null);
240: if (hdc == 0) {
241: // awt.16=Can not create DC for device
242: throw new RuntimeException(Messages.getString("awt.16")); //$NON-NLS-1$
243: }
244:
245: if (resolution == null) {
246: int width = win32.GetDeviceCaps(hdc,
247: WindowsDefs.LOGPIXELSY);
248: int height = win32.GetDeviceCaps(hdc,
249: WindowsDefs.LOGPIXELSX);
250: resolution = new Dimension(width, height);
251: }
252:
253: win32.DeleteDC(hdc);
254: }
255:
256: return resolution;
257: }
258: }
|