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.GraphicsConfiguration;
023: import java.awt.GraphicsDevice;
024: import java.awt.Rectangle;
025: import java.awt.Transparency;
026: import java.awt.geom.AffineTransform;
027: import java.awt.image.BufferedImage;
028: import java.awt.image.ColorModel;
029: import java.awt.image.DirectColorModel;
030: import java.awt.image.VolatileImage;
031:
032: import org.apache.harmony.awt.internal.nls.Messages;
033: import org.apache.harmony.awt.nativebridge.windows.Win32;
034: import org.apache.harmony.awt.nativebridge.windows.WindowsDefs;
035:
036: /**
037: * Windows GraphicsConfiguration implementation
038: *
039: */
040: public class WinGraphicsConfiguration extends GraphicsConfiguration {
041: private static final Win32 win32 = Win32.getInstance();
042:
043: private WinGraphicsDevice device;
044: private ColorModel cm;
045: private long flags;
046: private byte pixelType;
047:
048: private int bits = -1;
049: private byte redBits = -1;
050: private byte redShift = -1;
051: private int rmask = -1;
052: private byte greenBits = -1;
053: private byte greenShift = -1;
054: private int gmask = -1;
055: private byte blueBits = -1;
056: private byte blueShift = -1;
057: private int bmask = -1;
058: private byte alphaBits = -1;
059: private byte alphaShift = -1;
060: private int amask = -1;
061:
062: private int index;
063:
064: public WinGraphicsConfiguration(WinGraphicsDevice device,
065: int index, Win32.PIXELFORMATDESCRIPTOR pfd) {
066: this .device = device;
067: this .index = index;
068: init(pfd);
069: }
070:
071: public WinGraphicsConfiguration(long hwnd, long hdc) {
072: this .device = new WinGraphicsDevice(hwnd);
073: this .index = -1;
074:
075: int dci = win32.GetPixelFormat(hdc);
076: dci = (dci > 0) ? dci : 1;
077: Win32.PIXELFORMATDESCRIPTOR pfd = win32
078: .createPIXELFORMATDESCRIPTOR(false);
079: win32.DescribePixelFormat(hdc, dci, pfd.size(), pfd);
080: init(pfd);
081: pfd.free();
082: }
083:
084: /**
085: * Initializes private fileds with info from
086: * native PIXELFORMATDESCRIPTOR structure.
087: *
088: * @param pfd PIXELFORMATDESCRIPTOR structure.
089: */
090: private void init(Win32.PIXELFORMATDESCRIPTOR pfd) {
091: flags = pfd.get_dwFlags();
092: pixelType = pfd.get_iPixelType();
093: bits = pfd.get_cColorBits();
094:
095: if (bits == 0) {
096: return;
097: }
098:
099: if ((pixelType & WindowsDefs.PFD_TYPE_COLORINDEX) != WindowsDefs.PFD_TYPE_COLORINDEX) {
100: redBits = pfd.get_cRedBits();
101: redShift = pfd.get_cRedShift();
102: rmask = (int) (Math.pow(2, redBits) - 1) << redShift;
103:
104: greenBits = pfd.get_cGreenBits();
105: greenShift = pfd.get_cGreenShift();
106: gmask = (int) (Math.pow(2, greenBits) - 1) << greenShift;
107:
108: blueBits = pfd.get_cBlueBits();
109: blueShift = pfd.get_cBlueShift();
110: bmask = (int) (Math.pow(2, blueBits) - 1) << blueShift;
111:
112: alphaBits = pfd.get_cAlphaBits();
113: alphaShift = pfd.get_cAlphaShift();
114: amask = (int) (Math.pow(2, alphaBits) - 1) << alphaShift;
115: }
116:
117: long hdc = win32.CreateDCW(null, device.getIDstring(), null,
118: null);
119: cm = createColorModel(hdc);
120: win32.DeleteDC(hdc);
121:
122: }
123:
124: @Override
125: public GraphicsDevice getDevice() {
126: return device;
127: }
128:
129: @Override
130: public Rectangle getBounds() {
131: return device.getBounds();
132: }
133:
134: @Override
135: public AffineTransform getDefaultTransform() {
136: return new AffineTransform();
137: }
138:
139: @Override
140: public AffineTransform getNormalizingTransform() {
141: return new AffineTransform();
142: }
143:
144: @Override
145: public BufferedImage createCompatibleImage(int width, int height) {
146: return new BufferedImage(cm, cm.createCompatibleWritableRaster(
147: width, height), false, null);
148: }
149:
150: @Override
151: public BufferedImage createCompatibleImage(int width, int height,
152: int transparency) {
153: ColorModel cmt = getColorModel(transparency);
154: if (cmt == null) {
155: // awt.18=Transparency is not supported.
156: throw new IllegalArgumentException(Messages
157: .getString("awt.18")); //$NON-NLS-1$
158: }
159:
160: return new BufferedImage(cmt, cmt
161: .createCompatibleWritableRaster(width, height), false,
162: null);
163: }
164:
165: @Override
166: public ColorModel getColorModel() {
167: return cm;
168: }
169:
170: @Override
171: public ColorModel getColorModel(int transparency) {
172: switch (transparency) {
173: case Transparency.BITMASK:
174: return new DirectColorModel(25, 0xFF0000, 0xFF00, 0xFF,
175: 0x1000000);
176: case Transparency.TRANSLUCENT:
177: return ColorModel.getRGBdefault();
178: default:
179: return cm;
180: }
181: }
182:
183: @Override
184: public VolatileImage createCompatibleVolatileImage(int width,
185: int height) {
186: return new WinVolatileImage(this , width, height);
187: }
188:
189: @Override
190: public VolatileImage createCompatibleVolatileImage(int width,
191: int height, int transparency) {
192: return createCompatibleVolatileImage(width, height);
193: }
194:
195: public long getFlags() {
196: return flags;
197: }
198:
199: @Override
200: public boolean equals(Object obj) {
201: if (!(obj instanceof WinGraphicsConfiguration)) {
202: return false;
203: }
204:
205: WinGraphicsConfiguration gc = (WinGraphicsConfiguration) obj;
206:
207: // We do not use flags now. So GraphicsConfigurations with
208: // different flags are same for us.
209: //if (flags != gc.flags)
210: // return false;
211:
212: if (pixelType != gc.pixelType) {
213: return false;
214: }
215:
216: if (bits != gc.bits) {
217: return false;
218: }
219:
220: if (redBits != gc.redBits) {
221: return false;
222: }
223:
224: if (redShift != gc.redShift) {
225: return false;
226: }
227:
228: if (greenBits != gc.greenBits) {
229: return false;
230: }
231:
232: if (greenShift != gc.greenShift) {
233: return false;
234: }
235:
236: if (blueBits != gc.blueBits) {
237: return false;
238: }
239:
240: if (blueShift != gc.blueShift) {
241: return false;
242: }
243:
244: if (alphaBits != gc.alphaBits) {
245: return false;
246: }
247:
248: if (alphaShift != gc.alphaShift) {
249: return false;
250: }
251:
252: return true;
253: }
254:
255: public int getIndex() {
256: return index;
257: }
258:
259: private native ColorModel createColorModel(long hdc);
260: }
|