blob: 012c74af671ae83fd7d9ab87be965f0e5ca0b394 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.java2d.opengl;
27
28import java.awt.AWTException;
29import java.awt.BufferCapabilities;
30import java.awt.Component;
31import java.awt.Graphics;
32import java.awt.Image;
33import java.awt.ImageCapabilities;
34import java.awt.Transparency;
35import java.awt.color.ColorSpace;
36import java.awt.image.BufferedImage;
37import java.awt.image.ColorModel;
38import java.awt.image.DataBuffer;
39import java.awt.image.DirectColorModel;
40import java.awt.image.VolatileImage;
41import java.awt.image.WritableRaster;
42import sun.awt.X11ComponentPeer;
43import sun.awt.X11GraphicsConfig;
44import sun.awt.X11GraphicsDevice;
45import sun.awt.X11GraphicsEnvironment;
46import sun.awt.image.OffScreenImage;
47import sun.awt.image.SunVolatileImage;
48import sun.java2d.SurfaceData;
49
50public class GLXGraphicsConfig
51 extends X11GraphicsConfig
52 implements OGLGraphicsConfig
53{
54 private static ImageCapabilities imageCaps = new GLXImageCaps();
55 private BufferCapabilities bufferCaps;
56 private long pConfigInfo;
57 private int oglCaps;
58 private OGLContext context;
59
60 private static native long getGLXConfigInfo(int screennum, int visualnum);
61 private static native int getOGLCapabilities(long configInfo);
62 private native void initConfig(long aData, long ctxinfo);
63
64 private GLXGraphicsConfig(X11GraphicsDevice device, int visualnum,
65 long configInfo, int oglCaps)
66 {
67 super(device, visualnum, 0, 0,
68 (oglCaps & OGLContext.CAPS_DOUBLEBUFFERED) != 0);
69 pConfigInfo = configInfo;
70 initConfig(getAData(), configInfo);
71 this.oglCaps = oglCaps;
72 context = new OGLContext(OGLRenderQueue.getInstance());
73 }
74
75 public Object getProxyKey() {
76 return this;
77 }
78
79 public SurfaceData createManagedSurface(int w, int h, int transparency) {
80 return GLXSurfaceData.createData(this, w, h,
81 getColorModel(transparency),
82 null,
83 OGLSurfaceData.TEXTURE);
84 }
85
86 public static GLXGraphicsConfig getConfig(X11GraphicsDevice device,
87 int visualnum)
88 {
89 if (!X11GraphicsEnvironment.isGLXAvailable()) {
90 return null;
91 }
92
93 long cfginfo = 0;
94 OGLRenderQueue rq = OGLRenderQueue.getInstance();
95 rq.lock();
96 try {
97 // getGLXConfigInfo() creates and destroys temporary
98 // surfaces/contexts, so we should first invalidate the current
99 // Java-level context and flush the queue...
100 OGLContext.invalidateCurrentContext();
101 GLXGetConfigInfo action =
102 new GLXGetConfigInfo(device.getScreen(), visualnum);
103 rq.flushAndInvokeNow(action);
104 cfginfo = action.getConfigInfo();
105 } finally {
106 rq.unlock();
107 }
108 if (cfginfo == 0) {
109 return null;
110 }
111
112 int oglCaps = getOGLCapabilities(cfginfo);
113
114 return new GLXGraphicsConfig(device, visualnum, cfginfo, oglCaps);
115 }
116
117 /**
118 * This is a small helper class that allows us to execute
119 * getGLXConfigInfo() on the queue flushing thread.
120 */
121 private static class GLXGetConfigInfo implements Runnable {
122 private int screen;
123 private int visual;
124 private long cfginfo;
125 private GLXGetConfigInfo(int screen, int visual) {
126 this.screen = screen;
127 this.visual = visual;
128 }
129 public void run() {
130 cfginfo = getGLXConfigInfo(screen, visual);
131 }
132 public long getConfigInfo() {
133 return cfginfo;
134 }
135 }
136
137 /**
138 * Returns true if the provided capability bit is present for this config.
139 * See OGLContext.java for a list of supported capabilities.
140 */
141 public final boolean isCapPresent(int cap) {
142 return ((oglCaps & cap) != 0);
143 }
144
145 public final long getNativeConfigInfo() {
146 return pConfigInfo;
147 }
148
149 public final OGLContext getContext() {
150 return context;
151 }
152
153 @Override
154 public BufferedImage createCompatibleImage(int width, int height) {
155 ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
156 WritableRaster
157 raster = model.createCompatibleWritableRaster(width, height);
158 return new BufferedImage(model, raster, model.isAlphaPremultiplied(),
159 null);
160 }
161
162 @Override
163 public ColorModel getColorModel(int transparency) {
164 switch (transparency) {
165 case Transparency.OPAQUE:
166 // REMIND: once the ColorModel spec is changed, this should be
167 // an opaque premultiplied DCM...
168 return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
169 case Transparency.BITMASK:
170 return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
171 case Transparency.TRANSLUCENT:
172 ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
173 return new DirectColorModel(cs, 32,
174 0xff0000, 0xff00, 0xff, 0xff000000,
175 true, DataBuffer.TYPE_INT);
176 default:
177 return null;
178 }
179 }
180
181 public String toString() {
182 return ("GLXGraphicsConfig[dev="+screen+
183 ",vis=0x"+Integer.toHexString(visual)+
184 "]");
185 }
186
187 /**
188 * The following methods are invoked from MToolkit or XToolkit.java and
189 * X11ComponentPeer.java rather than having the X11-dependent
190 * implementations hardcoded in those classes. This way the appropriate
191 * actions are taken based on the peer's GraphicsConfig, whether it is
192 * an X11GraphicsConfig or a GLXGraphicsConfig.
193 */
194
195 /**
196 * Creates a new SurfaceData that will be associated with the given
197 * X11ComponentPeer.
198 */
199 @Override
200 public SurfaceData createSurfaceData(X11ComponentPeer peer) {
201 return GLXSurfaceData.createData(peer);
202 }
203
204 /**
205 * Creates a new hidden-acceleration image of the given width and height
206 * that is associated with the target Component.
207 */
208 @Override
209 public Image createAcceleratedImage(Component target,
210 int width, int height)
211 {
212 ColorModel model = getColorModel(Transparency.OPAQUE);
213 WritableRaster wr =
214 model.createCompatibleWritableRaster(width, height);
215 return new OffScreenImage(target, model, wr,
216 model.isAlphaPremultiplied());
217 }
218
219 /**
220 * The following methods correspond to the multibuffering methods in
221 * X11ComponentPeer.java...
222 */
223
224 /**
225 * Attempts to create a GLX-based backbuffer for the given peer. If
226 * the requested configuration is not natively supported, an AWTException
227 * is thrown. Otherwise, if the backbuffer creation is successful, a
228 * value of 1 is returned.
229 */
230 @Override
231 public long createBackBuffer(X11ComponentPeer peer,
232 int numBuffers, BufferCapabilities caps)
233 throws AWTException
234 {
235 if (numBuffers > 2) {
236 throw new AWTException(
237 "Only double or single buffering is supported");
238 }
239 BufferCapabilities configCaps = getBufferCapabilities();
240 if (!configCaps.isPageFlipping()) {
241 throw new AWTException("Page flipping is not supported");
242 }
243 if (caps.getFlipContents() == BufferCapabilities.FlipContents.PRIOR) {
244 throw new AWTException("FlipContents.PRIOR is not supported");
245 }
246
247 // non-zero return value means backbuffer creation was successful
248 // (checked in X11ComponentPeer.flip(), etc.)
249 return 1;
250 }
251
252 /**
253 * Destroys the backbuffer object represented by the given handle value.
254 */
255 @Override
256 public void destroyBackBuffer(long backBuffer) {
257 }
258
259 /**
260 * Creates a VolatileImage that essentially wraps the target Component's
261 * backbuffer (the provided backbuffer handle is essentially ignored).
262 */
263 @Override
264 public VolatileImage createBackBufferImage(Component target,
265 long backBuffer)
266 {
267 return new SunVolatileImage(target,
268 target.getWidth(), target.getHeight(),
269 Boolean.TRUE);
270 }
271
272 /**
273 * Performs the native GLX flip operation for the given target Component.
274 */
275 @Override
276 public void flip(X11ComponentPeer peer,
277 Component target, VolatileImage xBackBuffer,
278 BufferCapabilities.FlipContents flipAction)
279 {
280 if (flipAction == BufferCapabilities.FlipContents.COPIED) {
281 Graphics g = peer.getGraphics();
282 try {
283 g.drawImage(xBackBuffer, 0, 0, null);
284 } finally {
285 g.dispose();
286 }
287 return;
288 } else if (flipAction == BufferCapabilities.FlipContents.PRIOR) {
289 // not supported by GLX...
290 return;
291 }
292
293 OGLSurfaceData.swapBuffers(peer.getContentWindow());
294
295 if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
296 Graphics g = xBackBuffer.getGraphics();
297 try {
298 g.setColor(target.getBackground());
299 g.fillRect(0, 0,
300 xBackBuffer.getWidth(),
301 xBackBuffer.getHeight());
302 } finally {
303 g.dispose();
304 }
305 }
306 }
307
308 private static class GLXBufferCaps extends BufferCapabilities {
309 public GLXBufferCaps(boolean dblBuf) {
310 super(imageCaps, imageCaps,
311 dblBuf ? FlipContents.UNDEFINED : null);
312 }
313 }
314
315 @Override
316 public BufferCapabilities getBufferCapabilities() {
317 if (bufferCaps == null) {
318 bufferCaps = new GLXBufferCaps(isDoubleBuffered());
319 }
320 return bufferCaps;
321 }
322
323 private static class GLXImageCaps extends ImageCapabilities {
324 private GLXImageCaps() {
325 super(true);
326 }
327 public boolean isTrueVolatile() {
328 return true;
329 }
330 }
331
332 @Override
333 public ImageCapabilities getImageCapabilities() {
334 return imageCaps;
335 }
336}