XGraphicsConfiguration.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /* Copyright (C) 2000, 2003 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. package gnu.awt.xlib;
  7. import java.awt.GraphicsConfiguration;
  8. import java.awt.Rectangle;
  9. import java.awt.Graphics2D;
  10. import java.awt.Graphics;
  11. import java.awt.GraphicsDevice;
  12. import java.awt.Point;
  13. import java.awt.Color;
  14. import java.awt.color.ColorSpace;
  15. import java.awt.Font;
  16. import java.awt.image.*;
  17. import java.awt.geom.AffineTransform;
  18. import gnu.gcj.xlib.GC;
  19. import gnu.gcj.xlib.Drawable;
  20. import gnu.gcj.xlib.Window;
  21. import gnu.gcj.xlib.XImage;
  22. import gnu.gcj.xlib.Visual;
  23. import gnu.gcj.xlib.Colormap;
  24. import gnu.gcj.xlib.XColor;
  25. import gnu.gcj.xlib.Screen;
  26. import gnu.gcj.xlib.Display;
  27. import gnu.gcj.xlib.XException;
  28. import gnu.java.awt.Buffers;
  29. import java.util.Enumeration;
  30. import java.util.Hashtable;
  31. public class XGraphicsConfiguration extends GraphicsConfiguration
  32. {
  33. //public abstract GraphicsDevice getDevice();
  34. Visual visual;
  35. int format;
  36. Colormap colormap;
  37. ColorModel imageCM;
  38. ColorModel pixelCM;
  39. private static final int CACHE_SIZE_PER_DISPLAY = 10;
  40. static FontMetricsCache fontMetricsCache = new FontMetricsCache ();
  41. /** Font metrics cache class. Caches at most CACHE_SIZE_PER_DISPLAY
  42. * XFontMetrics objects for each display device. When a display's cache
  43. * gets full, the least-recently used entry is overwritten.
  44. * XXX: lruOrder rolls over after a few billion operations, so it might
  45. * on very rare occasions misinterpret which is the oldest entry
  46. */
  47. static class FontMetricsCache
  48. {
  49. private java.util.Hashtable displays = new java.util.Hashtable ();
  50. /** Font metrics cache for a display device
  51. */
  52. class PerDisplayCache
  53. {
  54. private int lruCount = 0;
  55. private java.util.Hashtable entries = new java.util.Hashtable ();
  56. class CacheEntry
  57. {
  58. int lruOrder;
  59. XFontMetrics fm;
  60. Font font;
  61. }
  62. /** Get an entry (null if not there) and update LRU ordering
  63. */
  64. XFontMetrics get (Font font)
  65. {
  66. CacheEntry entry = (CacheEntry)entries.get (font);
  67. if (entry != null)
  68. {
  69. entry.lruOrder = lruCount++;
  70. }
  71. return (entry==null) ? null : entry.fm;
  72. }
  73. /** Put an entry in the cache, eliminating the oldest entry if
  74. * the cache is at capacity.
  75. */
  76. void put (Font font, XFontMetrics fontMetrics)
  77. {
  78. if (entries.size () >= CACHE_SIZE_PER_DISPLAY)
  79. {
  80. // cache is full -- eliminate the oldest entry
  81. // slow operation, but shouldn't happen very often
  82. int maxAge = 0;
  83. CacheEntry oldestEntry = null;
  84. int referenceCount = lruCount;
  85. for (Enumeration e = entries.elements (); e.hasMoreElements ();)
  86. {
  87. CacheEntry entry = (CacheEntry)e.nextElement ();
  88. if ((referenceCount-entry.lruOrder) > maxAge)
  89. {
  90. maxAge = referenceCount-entry.lruOrder;
  91. oldestEntry = entry;
  92. }
  93. }
  94. if (oldestEntry != null)
  95. entries.remove (oldestEntry.font);
  96. }
  97. CacheEntry newEntry = new CacheEntry ();
  98. newEntry.lruOrder = lruCount++;
  99. newEntry.fm = fontMetrics;
  100. newEntry.font = font;
  101. entries.put (font,newEntry);
  102. }
  103. }
  104. /** Get the font metrics for a font, if it is present in the cache.
  105. * @param font The AWT font for which to find the font metrics
  106. * @param display The display, to select the cached entries for that display
  107. * @return The font metrics, or null if not cached
  108. */
  109. XFontMetrics get (Font font, Display display)
  110. {
  111. PerDisplayCache cache = (PerDisplayCache)displays.get (display);
  112. return (cache==null) ? null : cache.get (font);
  113. }
  114. /** Put a font in the cache
  115. * @param font The font
  116. * @param display The display
  117. * @param fontMetrics The font metrics
  118. */
  119. void put (Font font, Display display, XFontMetrics fontMetrics)
  120. {
  121. PerDisplayCache cache = (PerDisplayCache)displays.get (display);
  122. if (cache == null)
  123. {
  124. cache = new PerDisplayCache ();
  125. displays.put (display,cache);
  126. }
  127. cache.put (font,fontMetrics);
  128. }
  129. }
  130. public XGraphicsConfiguration(Visual visual)
  131. {
  132. this.visual = visual;
  133. }
  134. public BufferedImage createCompatibleImage(int width, int height)
  135. {
  136. XImage ximg = new XImage(visual, width, height,
  137. false // do not auto allocate memory
  138. );
  139. Point origin = new Point(0, 0);
  140. WritableRaster raster = createRasterForXImage(ximg, origin);
  141. /* This is not a good way of doing this. Multiple toolkits may
  142. want to share the BufferedImage. */
  143. Hashtable props = new Hashtable();
  144. props.put("gnu.gcj.xlib.XImage", ximg);
  145. props.put("java.awt.GraphicsConfiguration", this);
  146. BufferedImage bimg = new BufferedImage(imageCM,raster, false, props);
  147. DataBuffer dataB = raster.getDataBuffer();
  148. attachData(ximg, dataB, 0);
  149. return bimg;
  150. }
  151. WritableRaster createRasterForXImage(XImage ximage, Point origin)
  152. {
  153. if (imageCM == null) prepareColorModel(ximage);
  154. /*
  155. This will not work, since it creates a sample model that
  156. does not necessarily match the format of the XImage.
  157. WritableRaster raster =
  158. imageCM.createCompatibleWritableRaster(width, height); */
  159. // Create a sample model matching the XImage:
  160. SampleModel imageSM = null;
  161. int width = ximage.getWidth();
  162. int height = ximage.getHeight();
  163. int bitsPerPixel = ximage.getBitsPerPixel();
  164. int dataType =
  165. Buffers.smallestAppropriateTransferType(bitsPerPixel);
  166. int bitsPerDataElement = DataBuffer.getDataTypeSize(dataType);
  167. int scanlineStride = ximage.getBytesPerLine()*8/bitsPerDataElement;
  168. if (imageCM instanceof IndexColorModel)
  169. {
  170. int[] bandOffsets = {0};
  171. imageSM = new ComponentSampleModel(dataType,
  172. width, height,
  173. 1, // pixel stride
  174. scanlineStride,
  175. bandOffsets);
  176. }
  177. else if (imageCM instanceof PackedColorModel)
  178. {
  179. PackedColorModel pcm = (PackedColorModel) imageCM;
  180. int[] masks = pcm.getMasks();
  181. imageSM = new SinglePixelPackedSampleModel(dataType,
  182. width, height,
  183. scanlineStride,
  184. masks);
  185. }
  186. if (imageSM == null)
  187. {
  188. throw new UnsupportedOperationException("creating sample model " +
  189. "for " + imageCM +
  190. " not implemented");
  191. }
  192. WritableRaster raster = Raster.createWritableRaster(imageSM, origin);
  193. return raster;
  194. }
  195. /**
  196. * Attach a the memory of a data buffer to an XImage
  197. * structure. [This method is not gnu.awt.xlib specific, and should
  198. * maybe be moved to a different location.]
  199. *
  200. * @param offset Offset to data. The given offset does not include
  201. * data buffer offset, which will also be added.
  202. */
  203. static void attachData(XImage ximage, DataBuffer dataB, int offset)
  204. {
  205. offset += dataB.getOffset();
  206. switch (dataB.getDataType())
  207. {
  208. case DataBuffer.TYPE_BYTE:
  209. ximage.setData(((DataBufferByte) dataB).getData(), offset);
  210. break;
  211. case DataBuffer.TYPE_USHORT:
  212. ximage.setData(((DataBufferUShort) dataB).getData(), offset);
  213. break;
  214. case DataBuffer.TYPE_INT:
  215. ximage.setData(((DataBufferInt) dataB).getData(), offset);
  216. break;
  217. default:
  218. throw
  219. new UnsupportedOperationException("Do not know how to " +
  220. "set data for data " +
  221. "type " +
  222. dataB.getDataType());
  223. }
  224. }
  225. void prepareColorModel(XImage ximage)
  226. {
  227. format = ximage.getFormat();
  228. int bitsPerPixel = ximage.getBitsPerPixel();
  229. switch (format) {
  230. case XImage.ZPIXMAP_FORMAT:
  231. calcZPixmapModels(bitsPerPixel);
  232. break;
  233. default:
  234. throw new UnsupportedOperationException("unimplemented format");
  235. }
  236. }
  237. void calcZPixmapModels(int bitsPerPixel)
  238. {
  239. switch (visual.getVisualClass())
  240. {
  241. case Visual.VC_TRUE_COLOR:
  242. calcDecomposedRGBModels(bitsPerPixel);
  243. break;
  244. case Visual.VC_PSEUDO_COLOR:
  245. calcPseudoColorModels(bitsPerPixel);
  246. break;
  247. default:
  248. String msg = "unimplemented visual class";
  249. throw new UnsupportedOperationException(msg);
  250. }
  251. }
  252. void calcDecomposedRGBModels(int bitsPerPixel)
  253. {
  254. int dataType = Buffers.smallestAppropriateTransferType(bitsPerPixel);
  255. if (DataBuffer.getDataTypeSize(dataType) == bitsPerPixel)
  256. {
  257. ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
  258. imageCM = new DirectColorModel(cs,
  259. visual.getDepth(),
  260. visual.getRedMask(),
  261. visual.getGreenMask(),
  262. visual.getBlueMask(),
  263. 0, // no alpha
  264. false,
  265. dataType);
  266. }
  267. else
  268. {
  269. throw new
  270. UnsupportedOperationException("unimplemented bits per pixel");
  271. }
  272. }
  273. void calcPseudoColorModels(int bitsPerPixel)
  274. {
  275. if (colormap == null)
  276. colormap = visual.getScreen().getDefaultColormap();
  277. XColor[] colArray = colormap.getXColors();
  278. int numCol = colArray.length;
  279. byte[] rmap = new byte[numCol];
  280. byte[] gmap = new byte[numCol];
  281. byte[] bmap = new byte[numCol];
  282. byte[] amap = new byte[numCol];
  283. for (int i=0; i < numCol; i++)
  284. {
  285. XColor color = colArray[i];
  286. if (color.getFlags() == Colormap.FLAG_SHARED)
  287. {
  288. rmap[i] = (byte) (color.getRed() >> 8);
  289. gmap[i] = (byte) (color.getGreen() >> 8);
  290. bmap[i] = (byte) (color.getBlue() >> 8);
  291. amap[i] = (byte) 0xff;
  292. } // else, leave default zero values...
  293. }
  294. imageCM = new IndexColorModel(visual.getDepth(), numCol,
  295. rmap, gmap, bmap, amap);
  296. }
  297. /**
  298. * Gets the associated device that this configuration describes.
  299. *
  300. * @return the device
  301. */
  302. public GraphicsDevice getDevice()
  303. {
  304. throw new UnsupportedOperationException("not implemented");
  305. }
  306. /**
  307. * Returns a buffered image optimized to this device, so that blitting can
  308. * be supported in the buffered image.
  309. *
  310. * @param w the width of the buffer
  311. * @param h the height of the buffer
  312. * @return the buffered image, or null if none is supported
  313. */
  314. public BufferedImage createCompatibleImage(int width,
  315. int height,
  316. int transparency)
  317. {
  318. throw new UnsupportedOperationException("not implemented");
  319. }
  320. /**
  321. * Returns a buffered volatile image optimized to this device, so that
  322. * blitting can be supported in the buffered image. Because the buffer is
  323. * volatile, it can be optimized by native graphics accelerators.
  324. *
  325. * @param w the width of the buffer
  326. * @param h the height of the buffer
  327. * @return the buffered image, or null if none is supported
  328. * @see Component#createVolatileImage(int, int)
  329. * @since 1.4
  330. */
  331. public VolatileImage createCompatibleVolatileImage(int w, int h)
  332. {
  333. throw new UnsupportedOperationException("not implemented");
  334. }
  335. /**
  336. * FIXME: I'm not sure which color model that should be returned here.
  337. */
  338. public ColorModel getColorModel()
  339. {
  340. if (pixelCM == null)
  341. preparePixelCM();
  342. return pixelCM;
  343. }
  344. void preparePixelCM()
  345. {
  346. switch (visual.getVisualClass())
  347. {
  348. case Visual.VC_TRUE_COLOR:
  349. pixelCM = new DirectColorModel(visual.getDepth(),
  350. visual.getRedMask(),
  351. visual.getGreenMask(),
  352. visual.getBlueMask());
  353. break;
  354. case Visual.VC_PSEUDO_COLOR:
  355. if (colormap == null)
  356. colormap = visual.getScreen().getDefaultColormap();
  357. XColor[] colArray = colormap.getXColors();
  358. int numCol = colArray.length;
  359. byte[] rmap = new byte[numCol];
  360. byte[] gmap = new byte[numCol];
  361. byte[] bmap = new byte[numCol];
  362. byte[] amap = new byte[numCol];
  363. for (int i=0; i < numCol; i++)
  364. {
  365. XColor color = colArray[i];
  366. if (color.getFlags() == Colormap.FLAG_SHARED) {
  367. rmap[i] = (byte) (color.getRed() >> 8);
  368. gmap[i] = (byte) (color.getGreen() >> 8);
  369. bmap[i] = (byte) (color.getBlue() >> 8);
  370. amap[i] = (byte) 0xff;
  371. } // else, leave default zero values...
  372. }
  373. pixelCM = new IndexColorModel(visual.getDepth(), numCol,
  374. rmap, gmap, bmap, amap);
  375. break;
  376. default:
  377. throw new UnsupportedOperationException("not implemented");
  378. }
  379. }
  380. public ColorModel getColorModel(int transparency)
  381. {
  382. throw new UnsupportedOperationException("not implemented");
  383. }
  384. public AffineTransform getDefaultTransform()
  385. {
  386. throw new UnsupportedOperationException("not implemented");
  387. }
  388. public AffineTransform getNormalizingTransform()
  389. {
  390. throw new UnsupportedOperationException("not implemented");
  391. }
  392. public Rectangle getBounds()
  393. {
  394. throw new UnsupportedOperationException("not implemented");
  395. }
  396. Visual getVisual()
  397. {
  398. return visual;
  399. }
  400. /* FIXME: This should be moved to XGraphicsDevice... */
  401. XFontMetrics getXFontMetrics (java.awt.Font awtFont)
  402. {
  403. // If the metrics object for this font is already cached, use it.
  404. // Otherwise create and cache it.
  405. Display display = visual.getScreen ().getDisplay ();
  406. XFontMetrics fm = fontMetricsCache.get (awtFont,display);
  407. if (fm == null)
  408. {
  409. String foundry = "*";
  410. String family = awtFont.getName ();
  411. String weight = awtFont.isBold () ? "bold" : "medium";
  412. String slant = awtFont.isItalic () ? "i" : "r";
  413. String sWidth = "*";
  414. String addStyle = "";
  415. String pixelSize = "*";
  416. String pointSize = awtFont.getSize () + "0";
  417. String xres = "*";
  418. String yres = "*";
  419. String spacing = "*";
  420. String averageWidth = "*";
  421. String charset = "iso10646-1"; // because we use functions like XDrawString16
  422. String logicalFontDescription =
  423. "-" + // FontNameRegistry prefix
  424. foundry + "-" + family + "-" + weight + "-" +
  425. slant + "-" + sWidth + "-" + addStyle + "-" +
  426. pixelSize + "-" + pointSize + "-" + xres + "-" +
  427. yres + "-" + spacing + "-" + averageWidth + "-";
  428. // Try to load a Unicode font. If that doesn't work, try again, without
  429. // specifying the character set.
  430. try
  431. {
  432. gnu.gcj.xlib.Font xfont = new gnu.gcj.xlib.Font (display, logicalFontDescription + charset);
  433. fm = new XFontMetrics (xfont, awtFont);
  434. }
  435. catch (XException e)
  436. {
  437. gnu.gcj.xlib.Font xfont = new gnu.gcj.xlib.Font (display, logicalFontDescription + "*-*");
  438. fm = new XFontMetrics (xfont, awtFont);
  439. }
  440. fontMetricsCache.put (awtFont,display,fm);
  441. }
  442. return fm;
  443. }
  444. int getPixel(Color color)
  445. {
  446. /* FIXME: consider an integer technique whenever
  447. * the ColorModel is 8 bits per color.
  448. * The problem with using integers is that it doesn't work unless
  449. * the colors are 8 bits each (as in the array), since ColorModel.getDataElement(int[],int)
  450. * expects non-normalized values. For example, in a 16-bit display mode, you
  451. * would typically have 5 bits each for red and blue, and 6 bits for green.
  452. int[] components =
  453. {
  454. color.getRed (),
  455. color.getGreen (),
  456. color.getBlue (),
  457. 0xff
  458. };
  459. */
  460. int[] unnormalizedComponents = { 0, 0, 0, 0xff };
  461. ColorModel cm = getColorModel ();
  462. if (color != null)
  463. {
  464. float[] normalizedComponents =
  465. {
  466. ((float)color.getRed ()) / 255F,
  467. ((float)color.getGreen ()) / 255F,
  468. ((float)color.getBlue ()) / 255F,
  469. 1
  470. };
  471. cm.getUnnormalizedComponents(normalizedComponents, 0,
  472. unnormalizedComponents, 0);
  473. }
  474. return cm.getDataElement (unnormalizedComponents, 0);
  475. }
  476. /**
  477. * @since 1.5
  478. */
  479. public VolatileImage createCompatibleVolatileImage (int width, int height,
  480. int transparency)
  481. {
  482. return null;
  483. }
  484. }