001: /*
002: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.java2d.opengl;
027:
028: import java.awt.Component;
029: import java.awt.Container;
030: import java.awt.GraphicsConfiguration;
031: import java.awt.GraphicsDevice;
032: import java.awt.GraphicsEnvironment;
033: import java.awt.Image;
034: import java.awt.Insets;
035: import java.awt.Rectangle;
036: import java.awt.Transparency;
037: import java.awt.image.ColorModel;
038: import sun.awt.windows.WComponentPeer;
039: import sun.java2d.SurfaceData;
040: import sun.java2d.loops.SurfaceType;
041:
042: public abstract class WGLSurfaceData extends OGLSurfaceData {
043:
044: protected WComponentPeer peer;
045: private WGLGraphicsConfig graphicsConfig;
046:
047: private native void initOps(long pConfigInfo, long pPeerData,
048: int xoff, int yoff);
049:
050: protected native boolean initPbuffer(long pData, long pConfigInfo,
051: boolean isOpaque, int width, int height);
052:
053: protected WGLSurfaceData(WComponentPeer peer, WGLGraphicsConfig gc,
054: ColorModel cm, int type) {
055: super (gc, cm, type);
056: this .peer = peer;
057: this .graphicsConfig = gc;
058:
059: long pConfigInfo = gc.getNativeConfigInfo();
060: long pPeerData = 0L;
061: int xoff = 0, yoff = 0;
062: if (peer != null) {
063: Component c = (Component) peer.getTarget();
064: if (c instanceof Container) {
065: Insets insets = ((Container) c).getInsets();
066: xoff = -insets.left;
067: yoff = -insets.bottom;
068: }
069: pPeerData = peer.getData();
070: }
071:
072: initOps(pConfigInfo, pPeerData, xoff, yoff);
073: }
074:
075: public GraphicsConfiguration getDeviceConfiguration() {
076: return graphicsConfig;
077: }
078:
079: /**
080: * Creates a SurfaceData object representing the primary (front) buffer
081: * of an on-screen Window.
082: */
083: public static WGLWindowSurfaceData createData(WComponentPeer peer) {
084: WGLGraphicsConfig gc = getGC(peer);
085: return new WGLWindowSurfaceData(peer, gc);
086: }
087:
088: /**
089: * Creates a SurfaceData object representing the back buffer of a
090: * double-buffered on-screen Window.
091: */
092: public static WGLOffScreenSurfaceData createData(
093: WComponentPeer peer, Image image) {
094: WGLGraphicsConfig gc = getGC(peer);
095: Rectangle r = peer.getBounds();
096: return new WGLOffScreenSurfaceData(peer, gc, r.width, r.height,
097: image, peer.getColorModel(), FLIP_BACKBUFFER);
098: }
099:
100: /**
101: * Creates a SurfaceData object representing an off-screen buffer (either
102: * a Pbuffer or Texture).
103: */
104: public static WGLOffScreenSurfaceData createData(
105: WGLGraphicsConfig gc, int width, int height, ColorModel cm,
106: Image image, int type) {
107: return new WGLOffScreenSurfaceData(null, gc, width, height,
108: image, cm, type);
109: }
110:
111: public static WGLGraphicsConfig getGC(WComponentPeer peer) {
112: if (peer != null) {
113: return (WGLGraphicsConfig) peer.getGraphicsConfiguration();
114: } else {
115: // REMIND: this should rarely (never?) happen, but what if
116: // default config is not WGL?
117: GraphicsEnvironment env = GraphicsEnvironment
118: .getLocalGraphicsEnvironment();
119: GraphicsDevice gd = env.getDefaultScreenDevice();
120: return (WGLGraphicsConfig) gd.getDefaultConfiguration();
121: }
122: }
123:
124: public static class WGLWindowSurfaceData extends WGLSurfaceData {
125:
126: public WGLWindowSurfaceData(WComponentPeer peer,
127: WGLGraphicsConfig gc) {
128: super (peer, gc, peer.getColorModel(), WINDOW);
129: }
130:
131: public SurfaceData getReplacement() {
132: return peer.getSurfaceData();
133: }
134:
135: public Rectangle getBounds() {
136: Rectangle r = peer.getBounds();
137: r.x = r.y = 0;
138: return r;
139: }
140:
141: /**
142: * Returns destination Component associated with this SurfaceData.
143: */
144: public Object getDestination() {
145: return peer.getTarget();
146: }
147: }
148:
149: public static class WGLOffScreenSurfaceData extends WGLSurfaceData {
150:
151: private Image offscreenImage;
152: private int width, height;
153:
154: public WGLOffScreenSurfaceData(WComponentPeer peer,
155: WGLGraphicsConfig gc, int width, int height,
156: Image image, ColorModel cm, int type) {
157: super (peer, gc, cm, type);
158:
159: this .width = width;
160: this .height = height;
161: offscreenImage = image;
162:
163: initSurface(width, height);
164: }
165:
166: public SurfaceData getReplacement() {
167: return restoreContents(offscreenImage);
168: }
169:
170: public Rectangle getBounds() {
171: if (type == FLIP_BACKBUFFER) {
172: Rectangle r = peer.getBounds();
173: r.x = r.y = 0;
174: return r;
175: } else {
176: return new Rectangle(width, height);
177: }
178: }
179:
180: /**
181: * Returns destination Image associated with this SurfaceData.
182: */
183: public Object getDestination() {
184: return offscreenImage;
185: }
186: }
187: }
|