PageRenderTime 63ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/src/solaris/classes/sun/java2d/x11/X11Renderer.java

https://github.com/ikeji/openjdk7-jdk
Java | 525 lines | 433 code | 39 blank | 53 comment | 15 complexity | 46c363b4cf65e6e670f5cc24bc4e72e5 MD5 | raw file
  1. /*
  2. * Copyright (c) 2000, 2006, Oracle and/or its affiliates. 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. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package sun.java2d.x11;
  26. import java.awt.Polygon;
  27. import java.awt.Shape;
  28. import java.awt.geom.AffineTransform;
  29. import java.awt.geom.PathIterator;
  30. import java.awt.geom.Path2D;
  31. import java.awt.geom.IllegalPathStateException;
  32. import sun.awt.SunToolkit;
  33. import sun.java2d.SunGraphics2D;
  34. import sun.java2d.SurfaceData;
  35. import sun.java2d.loops.GraphicsPrimitive;
  36. import sun.java2d.pipe.Region;
  37. import sun.java2d.pipe.PixelDrawPipe;
  38. import sun.java2d.pipe.PixelFillPipe;
  39. import sun.java2d.pipe.ShapeDrawPipe;
  40. import sun.java2d.pipe.SpanIterator;
  41. import sun.java2d.pipe.ShapeSpanIterator;
  42. import sun.java2d.pipe.LoopPipe;
  43. public class X11Renderer implements
  44. PixelDrawPipe,
  45. PixelFillPipe,
  46. ShapeDrawPipe
  47. {
  48. public static X11Renderer getInstance() {
  49. return (GraphicsPrimitive.tracingEnabled()
  50. ? new X11TracingRenderer()
  51. : new X11Renderer());
  52. }
  53. private final long validate(SunGraphics2D sg2d) {
  54. // NOTE: getCompClip() will revalidateAll() if the
  55. // surfaceData is invalid. This should ensure that
  56. // the clip and pixel that we are validating against
  57. // are the most current.
  58. //
  59. // The assumption is that the pipeline after that
  60. // revalidation will either be another X11 pipe
  61. // (because the drawable format never changes on X11)
  62. // or a null pipeline if the surface is disposed.
  63. //
  64. // Since we do not get the ops structure of the SurfaceData
  65. // until the actual call down to the native level we will
  66. // pick up the most recently validated copy.
  67. // Note that if the surface is disposed, a NullSurfaceData
  68. // (with null native data structure) will be set in
  69. // sg2d, so we have to protect against it in native code.
  70. X11SurfaceData x11sd = (X11SurfaceData)sg2d.surfaceData;
  71. return x11sd.getRenderGC(sg2d.getCompClip(),
  72. sg2d.compositeState, sg2d.composite,
  73. sg2d.pixel);
  74. }
  75. native void XDrawLine(long pXSData, long xgc,
  76. int x1, int y1, int x2, int y2);
  77. public void drawLine(SunGraphics2D sg2d, int x1, int y1, int x2, int y2) {
  78. SunToolkit.awtLock();
  79. try {
  80. long xgc = validate(sg2d);
  81. int transx = sg2d.transX;
  82. int transy = sg2d.transY;
  83. XDrawLine(sg2d.surfaceData.getNativeOps(), xgc,
  84. x1+transx, y1+transy, x2+transx, y2+transy);
  85. } finally {
  86. SunToolkit.awtUnlock();
  87. }
  88. }
  89. native void XDrawRect(long pXSData, long xgc,
  90. int x, int y, int w, int h);
  91. public void drawRect(SunGraphics2D sg2d,
  92. int x, int y, int width, int height)
  93. {
  94. SunToolkit.awtLock();
  95. try {
  96. long xgc = validate(sg2d);
  97. XDrawRect(sg2d.surfaceData.getNativeOps(), xgc,
  98. x+sg2d.transX, y+sg2d.transY, width, height);
  99. } finally {
  100. SunToolkit.awtUnlock();
  101. }
  102. }
  103. native void XDrawRoundRect(long pXSData, long xgc,
  104. int x, int y, int w, int h,
  105. int arcW, int arcH);
  106. public void drawRoundRect(SunGraphics2D sg2d,
  107. int x, int y, int width, int height,
  108. int arcWidth, int arcHeight)
  109. {
  110. SunToolkit.awtLock();
  111. try {
  112. long xgc = validate(sg2d);
  113. XDrawRoundRect(sg2d.surfaceData.getNativeOps(), xgc,
  114. x+sg2d.transX, y+sg2d.transY, width, height,
  115. arcWidth, arcHeight);
  116. } finally {
  117. SunToolkit.awtUnlock();
  118. }
  119. }
  120. native void XDrawOval(long pXSData, long xgc,
  121. int x, int y, int w, int h);
  122. public void drawOval(SunGraphics2D sg2d,
  123. int x, int y, int width, int height)
  124. {
  125. SunToolkit.awtLock();
  126. try {
  127. long xgc = validate(sg2d);
  128. XDrawOval(sg2d.surfaceData.getNativeOps(), xgc,
  129. x+sg2d.transX, y+sg2d.transY, width, height);
  130. } finally {
  131. SunToolkit.awtUnlock();
  132. }
  133. }
  134. native void XDrawArc(long pXSData, long xgc,
  135. int x, int y, int w, int h,
  136. int angleStart, int angleExtent);
  137. public void drawArc(SunGraphics2D sg2d,
  138. int x, int y, int width, int height,
  139. int startAngle, int arcAngle)
  140. {
  141. SunToolkit.awtLock();
  142. try {
  143. long xgc = validate(sg2d);
  144. XDrawArc(sg2d.surfaceData.getNativeOps(), xgc,
  145. x+sg2d.transX, y+sg2d.transY, width, height,
  146. startAngle, arcAngle);
  147. } finally {
  148. SunToolkit.awtUnlock();
  149. }
  150. }
  151. native void XDrawPoly(long pXSData, long xgc,
  152. int transx, int transy,
  153. int[] xpoints, int[] ypoints,
  154. int npoints, boolean isclosed);
  155. public void drawPolyline(SunGraphics2D sg2d,
  156. int xpoints[], int ypoints[],
  157. int npoints)
  158. {
  159. SunToolkit.awtLock();
  160. try {
  161. long xgc = validate(sg2d);
  162. XDrawPoly(sg2d.surfaceData.getNativeOps(), xgc,
  163. sg2d.transX, sg2d.transY,
  164. xpoints, ypoints, npoints, false);
  165. } finally {
  166. SunToolkit.awtUnlock();
  167. }
  168. }
  169. public void drawPolygon(SunGraphics2D sg2d,
  170. int xpoints[], int ypoints[],
  171. int npoints)
  172. {
  173. SunToolkit.awtLock();
  174. try {
  175. long xgc = validate(sg2d);
  176. XDrawPoly(sg2d.surfaceData.getNativeOps(), xgc,
  177. sg2d.transX, sg2d.transY,
  178. xpoints, ypoints, npoints, true);
  179. } finally {
  180. SunToolkit.awtUnlock();
  181. }
  182. }
  183. native void XFillRect(long pXSData, long xgc,
  184. int x, int y, int w, int h);
  185. public void fillRect(SunGraphics2D sg2d,
  186. int x, int y, int width, int height)
  187. {
  188. SunToolkit.awtLock();
  189. try {
  190. long xgc = validate(sg2d);
  191. XFillRect(sg2d.surfaceData.getNativeOps(), xgc,
  192. x+sg2d.transX, y+sg2d.transY, width, height);
  193. } finally {
  194. SunToolkit.awtUnlock();
  195. }
  196. }
  197. native void XFillRoundRect(long pXSData, long xgc,
  198. int x, int y, int w, int h,
  199. int arcW, int arcH);
  200. public void fillRoundRect(SunGraphics2D sg2d,
  201. int x, int y, int width, int height,
  202. int arcWidth, int arcHeight)
  203. {
  204. SunToolkit.awtLock();
  205. try {
  206. long xgc = validate(sg2d);
  207. XFillRoundRect(sg2d.surfaceData.getNativeOps(), xgc,
  208. x+sg2d.transX, y+sg2d.transY, width, height,
  209. arcWidth, arcHeight);
  210. } finally {
  211. SunToolkit.awtUnlock();
  212. }
  213. }
  214. native void XFillOval(long pXSData, long xgc,
  215. int x, int y, int w, int h);
  216. public void fillOval(SunGraphics2D sg2d,
  217. int x, int y, int width, int height)
  218. {
  219. SunToolkit.awtLock();
  220. try {
  221. long xgc = validate(sg2d);
  222. XFillOval(sg2d.surfaceData.getNativeOps(), xgc,
  223. x+sg2d.transX, y+sg2d.transY, width, height);
  224. } finally {
  225. SunToolkit.awtUnlock();
  226. }
  227. }
  228. native void XFillArc(long pXSData, long xgc,
  229. int x, int y, int w, int h,
  230. int angleStart, int angleExtent);
  231. public void fillArc(SunGraphics2D sg2d,
  232. int x, int y, int width, int height,
  233. int startAngle, int arcAngle)
  234. {
  235. SunToolkit.awtLock();
  236. try {
  237. long xgc = validate(sg2d);
  238. XFillArc(sg2d.surfaceData.getNativeOps(), xgc,
  239. x+sg2d.transX, y+sg2d.transY, width, height,
  240. startAngle, arcAngle);
  241. } finally {
  242. SunToolkit.awtUnlock();
  243. }
  244. }
  245. native void XFillPoly(long pXSData, long xgc,
  246. int transx, int transy,
  247. int[] xpoints, int[] ypoints,
  248. int npoints);
  249. public void fillPolygon(SunGraphics2D sg2d,
  250. int xpoints[], int ypoints[],
  251. int npoints)
  252. {
  253. SunToolkit.awtLock();
  254. try {
  255. long xgc = validate(sg2d);
  256. XFillPoly(sg2d.surfaceData.getNativeOps(), xgc,
  257. sg2d.transX, sg2d.transY, xpoints, ypoints, npoints);
  258. } finally {
  259. SunToolkit.awtUnlock();
  260. }
  261. }
  262. native void XFillSpans(long pXSData, long xgc,
  263. SpanIterator si, long iterator,
  264. int transx, int transy);
  265. native void XDoPath(SunGraphics2D sg2d, long pXSData, long xgc,
  266. int transX, int transY, Path2D.Float p2df,
  267. boolean isFill);
  268. private void doPath(SunGraphics2D sg2d, Shape s, boolean isFill) {
  269. Path2D.Float p2df;
  270. int transx, transy;
  271. if (sg2d.transformState <= sg2d.TRANSFORM_INT_TRANSLATE) {
  272. if (s instanceof Path2D.Float) {
  273. p2df = (Path2D.Float)s;
  274. } else {
  275. p2df = new Path2D.Float(s);
  276. }
  277. transx = sg2d.transX;
  278. transy = sg2d.transY;
  279. } else {
  280. p2df = new Path2D.Float(s, sg2d.transform);
  281. transx = 0;
  282. transy = 0;
  283. }
  284. SunToolkit.awtLock();
  285. try {
  286. long xgc = validate(sg2d);
  287. XDoPath(sg2d, sg2d.surfaceData.getNativeOps(), xgc,
  288. transx, transy, p2df, isFill);
  289. } finally {
  290. SunToolkit.awtUnlock();
  291. }
  292. }
  293. public void draw(SunGraphics2D sg2d, Shape s) {
  294. if (sg2d.strokeState == sg2d.STROKE_THIN) {
  295. // Delegate to drawPolygon() if possible...
  296. if (s instanceof Polygon &&
  297. sg2d.transformState < sg2d.TRANSFORM_TRANSLATESCALE)
  298. {
  299. Polygon p = (Polygon) s;
  300. drawPolygon(sg2d, p.xpoints, p.ypoints, p.npoints);
  301. return;
  302. }
  303. // Otherwise we will use drawPath() for
  304. // high-quality thin paths.
  305. doPath(sg2d, s, false);
  306. } else if (sg2d.strokeState < sg2d.STROKE_CUSTOM) {
  307. // REMIND: X11 can handle uniform scaled wide lines
  308. // and dashed lines itself if we set the appropriate
  309. // XGC attributes (TBD).
  310. ShapeSpanIterator si = LoopPipe.getStrokeSpans(sg2d, s);
  311. try {
  312. SunToolkit.awtLock();
  313. try {
  314. long xgc = validate(sg2d);
  315. XFillSpans(sg2d.surfaceData.getNativeOps(), xgc,
  316. si, si.getNativeIterator(),
  317. 0, 0);
  318. } finally {
  319. SunToolkit.awtUnlock();
  320. }
  321. } finally {
  322. si.dispose();
  323. }
  324. } else {
  325. fill(sg2d, sg2d.stroke.createStrokedShape(s));
  326. }
  327. }
  328. public void fill(SunGraphics2D sg2d, Shape s) {
  329. if (sg2d.strokeState == sg2d.STROKE_THIN) {
  330. // Delegate to fillPolygon() if possible...
  331. if (s instanceof Polygon &&
  332. sg2d.transformState < sg2d.TRANSFORM_TRANSLATESCALE)
  333. {
  334. Polygon p = (Polygon) s;
  335. fillPolygon(sg2d, p.xpoints, p.ypoints, p.npoints);
  336. return;
  337. }
  338. // Otherwise we will use fillPath() for
  339. // high-quality fills.
  340. doPath(sg2d, s, true);
  341. return;
  342. }
  343. AffineTransform at;
  344. int transx, transy;
  345. if (sg2d.transformState < sg2d.TRANSFORM_TRANSLATESCALE) {
  346. // Transform (translation) will be done by XFillSpans
  347. at = null;
  348. transx = sg2d.transX;
  349. transy = sg2d.transY;
  350. } else {
  351. // Transform will be done by the PathIterator
  352. at = sg2d.transform;
  353. transx = transy = 0;
  354. }
  355. ShapeSpanIterator ssi = LoopPipe.getFillSSI(sg2d);
  356. try {
  357. // Subtract transx/y from the SSI clip to match the
  358. // (potentially untranslated) geometry fed to it
  359. Region clip = sg2d.getCompClip();
  360. ssi.setOutputAreaXYXY(clip.getLoX() - transx,
  361. clip.getLoY() - transy,
  362. clip.getHiX() - transx,
  363. clip.getHiY() - transy);
  364. ssi.appendPath(s.getPathIterator(at));
  365. SunToolkit.awtLock();
  366. try {
  367. long xgc = validate(sg2d);
  368. XFillSpans(sg2d.surfaceData.getNativeOps(), xgc,
  369. ssi, ssi.getNativeIterator(),
  370. transx, transy);
  371. } finally {
  372. SunToolkit.awtUnlock();
  373. }
  374. } finally {
  375. ssi.dispose();
  376. }
  377. }
  378. native void devCopyArea(long sdOps, long xgc,
  379. int srcx, int srcy,
  380. int dstx, int dsty,
  381. int w, int h);
  382. public static class X11TracingRenderer extends X11Renderer {
  383. void XDrawLine(long pXSData, long xgc,
  384. int x1, int y1, int x2, int y2)
  385. {
  386. GraphicsPrimitive.tracePrimitive("X11DrawLine");
  387. super.XDrawLine(pXSData, xgc, x1, y1, x2, y2);
  388. }
  389. void XDrawRect(long pXSData, long xgc,
  390. int x, int y, int w, int h)
  391. {
  392. GraphicsPrimitive.tracePrimitive("X11DrawRect");
  393. super.XDrawRect(pXSData, xgc, x, y, w, h);
  394. }
  395. void XDrawRoundRect(long pXSData, long xgc,
  396. int x, int y, int w, int h,
  397. int arcW, int arcH)
  398. {
  399. GraphicsPrimitive.tracePrimitive("X11DrawRoundRect");
  400. super.XDrawRoundRect(pXSData, xgc, x, y, w, h, arcW, arcH);
  401. }
  402. void XDrawOval(long pXSData, long xgc,
  403. int x, int y, int w, int h)
  404. {
  405. GraphicsPrimitive.tracePrimitive("X11DrawOval");
  406. super.XDrawOval(pXSData, xgc, x, y, w, h);
  407. }
  408. void XDrawArc(long pXSData, long xgc,
  409. int x, int y, int w, int h,
  410. int angleStart, int angleExtent)
  411. {
  412. GraphicsPrimitive.tracePrimitive("X11DrawArc");
  413. super.XDrawArc(pXSData, xgc,
  414. x, y, w, h, angleStart, angleExtent);
  415. }
  416. void XDrawPoly(long pXSData, long xgc,
  417. int transx, int transy,
  418. int[] xpoints, int[] ypoints,
  419. int npoints, boolean isclosed)
  420. {
  421. GraphicsPrimitive.tracePrimitive("X11DrawPoly");
  422. super.XDrawPoly(pXSData, xgc, transx, transy,
  423. xpoints, ypoints, npoints, isclosed);
  424. }
  425. void XDoPath(SunGraphics2D sg2d, long pXSData, long xgc,
  426. int transX, int transY, Path2D.Float p2df,
  427. boolean isFill)
  428. {
  429. GraphicsPrimitive.tracePrimitive(isFill ?
  430. "X11FillPath" :
  431. "X11DrawPath");
  432. super.XDoPath(sg2d, pXSData, xgc, transX, transY, p2df, isFill);
  433. }
  434. void XFillRect(long pXSData, long xgc,
  435. int x, int y, int w, int h)
  436. {
  437. GraphicsPrimitive.tracePrimitive("X11FillRect");
  438. super.XFillRect(pXSData, xgc, x, y, w, h);
  439. }
  440. void XFillRoundRect(long pXSData, long xgc,
  441. int x, int y, int w, int h,
  442. int arcW, int arcH)
  443. {
  444. GraphicsPrimitive.tracePrimitive("X11FillRoundRect");
  445. super.XFillRoundRect(pXSData, xgc, x, y, w, h, arcW, arcH);
  446. }
  447. void XFillOval(long pXSData, long xgc,
  448. int x, int y, int w, int h)
  449. {
  450. GraphicsPrimitive.tracePrimitive("X11FillOval");
  451. super.XFillOval(pXSData, xgc, x, y, w, h);
  452. }
  453. void XFillArc(long pXSData, long xgc,
  454. int x, int y, int w, int h,
  455. int angleStart, int angleExtent)
  456. {
  457. GraphicsPrimitive.tracePrimitive("X11FillArc");
  458. super.XFillArc(pXSData, xgc,
  459. x, y, w, h, angleStart, angleExtent);
  460. }
  461. void XFillPoly(long pXSData, long xgc,
  462. int transx, int transy,
  463. int[] xpoints, int[] ypoints,
  464. int npoints)
  465. {
  466. GraphicsPrimitive.tracePrimitive("X11FillPoly");
  467. super.XFillPoly(pXSData, xgc,
  468. transx, transy, xpoints, ypoints, npoints);
  469. }
  470. void XFillSpans(long pXSData, long xgc,
  471. SpanIterator si, long iterator, int transx, int transy)
  472. {
  473. GraphicsPrimitive.tracePrimitive("X11FillSpans");
  474. super.XFillSpans(pXSData, xgc,
  475. si, iterator, transx, transy);
  476. }
  477. void devCopyArea(long sdOps, long xgc,
  478. int srcx, int srcy,
  479. int dstx, int dsty,
  480. int w, int h)
  481. {
  482. GraphicsPrimitive.tracePrimitive("X11CopyArea");
  483. super.devCopyArea(sdOps, xgc, srcx, srcy, dstx, dsty, w, h);
  484. }
  485. }
  486. }