|
1 /* |
|
2 * Copyright (c) 2019, 2019, 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 |
|
26 package sun.java2d.metal; |
|
27 |
|
28 import java.awt.*; |
|
29 |
|
30 import sun.java2d.SurfaceData; |
|
31 |
|
32 import java.awt.Composite; |
|
33 import sun.java2d.loops.CompositeType; |
|
34 import sun.java2d.loops.GraphicsPrimitive; |
|
35 import java.awt.image.ColorModel; |
|
36 import java.awt.image.Raster; |
|
37 import sun.java2d.loops.SurfaceType; |
|
38 import sun.java2d.metal.MetalLayer; |
|
39 import sun.awt.SunHints; |
|
40 import sun.java2d.metal.MetalGraphicsConfig; |
|
41 import sun.java2d.opengl.CGLGraphicsConfig; |
|
42 import sun.java2d.opengl.CGLLayer; |
|
43 import sun.java2d.opengl.CGLSurfaceData; |
|
44 import sun.java2d.opengl.OGLRenderQueue; |
|
45 import sun.lwawt.macosx.CPlatformView; |
|
46 import sun.java2d.pipe.hw.AccelSurface; |
|
47 import sun.java2d.metal.MetalRenderer; |
|
48 import sun.java2d.metal.MetalRenderQueue; |
|
49 import sun.java2d.pipe.PixelToParallelogramConverter; |
|
50 import sun.java2d.pipe.ParallelogramPipe; |
|
51 import sun.java2d.SunGraphics2D; |
|
52 |
|
53 public class MetalSurfaceData extends SurfaceData |
|
54 implements AccelSurface { |
|
55 //protected final int scale; |
|
56 protected final int width; |
|
57 protected final int height; |
|
58 protected CPlatformView pView; |
|
59 private MetalGraphicsConfig graphicsConfig; |
|
60 |
|
61 //private MetalGraphicsConfig graphicsConfig; |
|
62 private int nativeWidth, nativeHeight; |
|
63 protected int type; |
|
64 protected static ParallelogramPipe mtlAAPgramPipe; |
|
65 protected static MetalRenderer mtlRenderPipe; |
|
66 protected static PixelToParallelogramConverter mtlTxRenderPipe; |
|
67 |
|
68 private native int getTextureTarget(long pData); |
|
69 private native int getTextureID(long pData); |
|
70 protected native boolean initTexture(long pData, |
|
71 boolean isOpaque, |
|
72 int width, int height); |
|
73 protected native void clearWindow(); |
|
74 |
|
75 static { |
|
76 if (!GraphicsEnvironment.isHeadless()) { |
|
77 MetalRenderQueue rq = MetalRenderQueue.getInstance(); |
|
78 mtlRenderPipe = new MetalRenderer(rq); |
|
79 |
|
80 mtlAAPgramPipe = mtlRenderPipe.getAAParallelogramPipe(); |
|
81 |
|
82 mtlTxRenderPipe = |
|
83 new PixelToParallelogramConverter(mtlRenderPipe, |
|
84 mtlRenderPipe, |
|
85 1.0, 0.25, true); |
|
86 } |
|
87 } |
|
88 |
|
89 native void validate(int xoff, int yoff, int width, int height, boolean isOpaque); |
|
90 |
|
91 private native void initOps(long pConfigInfo, long pPeerData, long layerPtr, |
|
92 int xoff, int yoff, boolean isOpaque); |
|
93 |
|
94 MetalSurfaceData(MetalGraphicsConfig gc, ColorModel cm, int type, |
|
95 int width, int height) { |
|
96 // TODO : Map the coming type to proper custom type and call super() |
|
97 //super(gc, cm, type); |
|
98 super(SurfaceType.Any3Byte, cm ); |
|
99 // TEXTURE shouldn't be scaled, it is used for managed BufferedImages. |
|
100 // TODO : We need to set scale factor |
|
101 //scale = type == TEXTURE ? 1 : gc.getDevice().getScaleFactor(); |
|
102 this.width = width ;// * scale; |
|
103 this.height = height;// * scale; |
|
104 |
|
105 graphicsConfig = gc; |
|
106 } |
|
107 |
|
108 protected MetalSurfaceData(CPlatformView pView, MetalGraphicsConfig gc, |
|
109 ColorModel cm, int type, int width, int height) |
|
110 { |
|
111 this(gc, cm, type, width, height); |
|
112 this.pView = pView; |
|
113 this.graphicsConfig = gc; |
|
114 |
|
115 // TODO : Check whether we need native config info here |
|
116 long pConfigInfo = gc.getNativeConfigInfo(); |
|
117 long pPeerData = 0L; |
|
118 boolean isOpaque = true; |
|
119 if (pView != null) { |
|
120 pPeerData = pView.getAWTView(); |
|
121 isOpaque = pView.isOpaque(); |
|
122 } |
|
123 // TODO : check initOps logic it is native is OGL |
|
124 initOps(pConfigInfo, pPeerData, 0, 0, 0, isOpaque); |
|
125 } |
|
126 |
|
127 protected MetalSurfaceData(MetalLayer layer, MetalGraphicsConfig gc, |
|
128 ColorModel cm, int type, int width, int height) |
|
129 { |
|
130 this(gc, cm, type, width, height); |
|
131 this.graphicsConfig = gc; |
|
132 |
|
133 long pConfigInfo = gc.getNativeConfigInfo(); |
|
134 long layerPtr = 0L; |
|
135 boolean isOpaque = true; |
|
136 if (layer != null) { |
|
137 layerPtr = layer.getPointer(); |
|
138 isOpaque = layer.isOpaque(); |
|
139 } |
|
140 initOps(pConfigInfo, 0, layerPtr, 0, 0, isOpaque); |
|
141 } |
|
142 |
|
143 public GraphicsConfiguration getDeviceConfiguration() { |
|
144 return graphicsConfig; //dummy |
|
145 } |
|
146 |
|
147 public static MetalWindowSurfaceData createData(CPlatformView pView) { |
|
148 MetalGraphicsConfig gc = getGC(pView); |
|
149 return new MetalWindowSurfaceData(pView, gc); |
|
150 } |
|
151 |
|
152 public static MetalSurfaceData createData(MetalLayer layer) { |
|
153 MetalGraphicsConfig gc = getGC(layer); |
|
154 Rectangle r = layer.getBounds(); |
|
155 //return new MetalSurfaceData( gc, gc.getColorModel(), 1, r.width, r.height); |
|
156 return new MetalLayerSurfaceData(layer, gc, r.width, r.height); |
|
157 } |
|
158 |
|
159 public static MetalGraphicsConfig getGC(CPlatformView pView) { |
|
160 if (pView != null) { |
|
161 return (MetalGraphicsConfig)pView.getGraphicsConfiguration(); |
|
162 } else { |
|
163 // REMIND: this should rarely (never?) happen, but what if |
|
164 // default config is not CGL? |
|
165 GraphicsEnvironment env = GraphicsEnvironment |
|
166 .getLocalGraphicsEnvironment(); |
|
167 GraphicsDevice gd = env.getDefaultScreenDevice(); |
|
168 return (MetalGraphicsConfig) gd.getDefaultConfiguration(); |
|
169 } |
|
170 } |
|
171 |
|
172 public static MetalGraphicsConfig getGC(MetalLayer layer) { |
|
173 return (MetalGraphicsConfig)layer.getGraphicsConfiguration(); |
|
174 } |
|
175 |
|
176 public void validate() { |
|
177 // Overridden in MetalWindowSurfaceData below |
|
178 } |
|
179 |
|
180 public Rectangle getNativeBounds() { |
|
181 MetalRenderQueue rq = MetalRenderQueue.getInstance(); |
|
182 rq.lock(); |
|
183 try { |
|
184 return new Rectangle(nativeWidth, nativeHeight); |
|
185 } finally { |
|
186 rq.unlock(); |
|
187 } |
|
188 } |
|
189 |
|
190 public long getNativeResource(int resType) { |
|
191 if (resType == TEXTURE) { |
|
192 return getTextureID(); |
|
193 } |
|
194 return 0L; |
|
195 } |
|
196 |
|
197 public Object getDestination() { |
|
198 return this; //dummy |
|
199 } |
|
200 |
|
201 |
|
202 //Returns one of the surface type constants defined above. |
|
203 |
|
204 public final int getType() { |
|
205 return type; |
|
206 } |
|
207 |
|
208 // |
|
209 // If this surface is backed by a texture object, returns the target |
|
210 // for that texture (either GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE_ARB). |
|
211 /// Otherwise, this method will return zero. |
|
212 public final int getTextureTarget() { |
|
213 return getTextureTarget(getNativeOps()); |
|
214 } |
|
215 |
|
216 // |
|
217 // If this surface is backed by a texture object, returns the texture ID |
|
218 // for that texture. |
|
219 //Otherwise, this method will return zero. |
|
220 // |
|
221 public final int getTextureID() { |
|
222 return getTextureID(getNativeOps()); |
|
223 } |
|
224 |
|
225 // Returns the MetalContext for the GraphicsConfig associated with this |
|
226 //surface. |
|
227 public final MetalContext getContext() { |
|
228 return graphicsConfig.getContext(); |
|
229 } |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 public void validatePipe(SunGraphics2D sg2d) { |
|
235 //TextPipe textpipe; |
|
236 //boolean validated = false; |
|
237 |
|
238 // OGLTextRenderer handles both AA and non-AA text, but |
|
239 // only works with the following modes: |
|
240 // (Note: For LCD text we only enter this code path if |
|
241 // canRenderLCDText() has already validated that the mode is |
|
242 // CompositeType.SrcNoEa (opaque color), which will be subsumed |
|
243 // by the CompositeType.SrcNoEa (any color) test below.) |
|
244 |
|
245 // Copy block from OGLSurfaceData |
|
246 //textpipe = sg2d.textpipe; //tmp |
|
247 |
|
248 PixelToParallelogramConverter txPipe = null; |
|
249 MetalRenderer nonTxPipe = null; |
|
250 |
|
251 if (sg2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) { |
|
252 if (sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR) { |
|
253 if (sg2d.compositeState <= SunGraphics2D.COMP_XOR) { |
|
254 txPipe = mtlTxRenderPipe; |
|
255 nonTxPipe = mtlRenderPipe; |
|
256 } |
|
257 } else if (sg2d.compositeState <= SunGraphics2D.COMP_ALPHA) { |
|
258 //if (OGLPaints.isValid(sg2d)) { |
|
259 txPipe = mtlTxRenderPipe; |
|
260 nonTxPipe = mtlRenderPipe; |
|
261 //} |
|
262 // custom paints handled by super.validatePipe() below |
|
263 } |
|
264 } else { |
|
265 if (sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR) { |
|
266 if (//graphicsConfig.isCapPresent(CAPS_PS30) && |
|
267 (sg2d.imageComp == CompositeType.SrcOverNoEa || |
|
268 sg2d.imageComp == CompositeType.SrcOver)) |
|
269 { |
|
270 //if (!validated) { |
|
271 super.validatePipe(sg2d); |
|
272 //validated = true; |
|
273 //} |
|
274 PixelToParallelogramConverter aaConverter = |
|
275 new PixelToParallelogramConverter(sg2d.shapepipe, |
|
276 mtlAAPgramPipe, |
|
277 1.0/8.0, 0.499, |
|
278 false); |
|
279 sg2d.drawpipe = aaConverter; |
|
280 sg2d.fillpipe = aaConverter; |
|
281 sg2d.shapepipe = aaConverter; |
|
282 } else if (sg2d.compositeState == SunGraphics2D.COMP_XOR) { |
|
283 // install the solid pipes when AA and XOR are both enabled |
|
284 txPipe = mtlTxRenderPipe; |
|
285 nonTxPipe = mtlRenderPipe; |
|
286 } |
|
287 } |
|
288 // other cases handled by super.validatePipe() below |
|
289 } |
|
290 |
|
291 if (txPipe != null) { |
|
292 if (sg2d.transformState >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) { |
|
293 sg2d.drawpipe = txPipe; |
|
294 sg2d.fillpipe = txPipe; |
|
295 } else if (sg2d.strokeState != SunGraphics2D.STROKE_THIN) { |
|
296 sg2d.drawpipe = txPipe; |
|
297 sg2d.fillpipe = nonTxPipe; |
|
298 } else { |
|
299 sg2d.drawpipe = nonTxPipe; |
|
300 sg2d.fillpipe = nonTxPipe; |
|
301 } |
|
302 // Note that we use the transforming pipe here because it |
|
303 // will examine the shape and possibly perform an optimized |
|
304 // operation if it can be simplified. The simplifications |
|
305 // will be valid for all STROKE and TRANSFORM types. |
|
306 sg2d.shapepipe = txPipe; |
|
307 } else { |
|
308 |
|
309 super.validatePipe(sg2d); |
|
310 |
|
311 } |
|
312 |
|
313 // install the text pipe based on our earlier decision |
|
314 //sg2d.textpipe = textpipe; |
|
315 |
|
316 // always override the image pipe with the specialized OGL pipe |
|
317 // TODO : We dont override image pipe with MetalImagePipe. |
|
318 // this needs to be implemented. |
|
319 sg2d.imagepipe = imagepipe; |
|
320 } |
|
321 |
|
322 public SurfaceData getReplacement() { |
|
323 return this; //dummy |
|
324 } |
|
325 |
|
326 /* |
|
327 * TODO : In case of OpenGL their is no getRaster() |
|
328 * implementation in CGLSurfaceData or OGLSurfaceData. |
|
329 * Needs more verification. |
|
330 */ |
|
331 public Raster getRaster(int x, int y, int w, int h) { |
|
332 throw new InternalError("not implemented yet"); |
|
333 //System.out.println("MetalSurfaceData -- getRaster() not implemented yet"); |
|
334 } |
|
335 |
|
336 public Rectangle getBounds() { |
|
337 //Rectangle r = pView.getBounds(); |
|
338 return new Rectangle(0, 0, width, height); |
|
339 } |
|
340 |
|
341 protected void initSurface(final int width, final int height) { |
|
342 MetalRenderQueue rq = MetalRenderQueue.getInstance(); |
|
343 rq.lock(); |
|
344 try { |
|
345 rq.flushAndInvokeNow(new Runnable() { |
|
346 public void run() { |
|
347 initSurfaceNow(width, height); |
|
348 } |
|
349 }); |
|
350 } finally { |
|
351 rq.unlock(); |
|
352 } |
|
353 } |
|
354 |
|
355 private void initSurfaceNow(int width, int height) { |
|
356 boolean isOpaque = (getTransparency() == Transparency.OPAQUE); |
|
357 boolean success = false; |
|
358 |
|
359 /*switch (type) { |
|
360 case TEXTURE: |
|
361 success = initTexture(getNativeOps(), |
|
362 isOpaque, isTexNonPow2Available(), |
|
363 isTexRectAvailable(), |
|
364 width, height); |
|
365 break; |
|
366 |
|
367 case FBOBJECT: |
|
368 success = initFBObject(getNativeOps(), |
|
369 isOpaque, isTexNonPow2Available(), |
|
370 isTexRectAvailable(), |
|
371 width, height); |
|
372 break; |
|
373 |
|
374 case FLIP_BACKBUFFER: |
|
375 success = initFlipBackbuffer(getNativeOps()); |
|
376 break; |
|
377 |
|
378 default: |
|
379 break; |
|
380 }*/ |
|
381 |
|
382 success = initTexture(getNativeOps(), |
|
383 isOpaque, |
|
384 width, height); |
|
385 if (!success) { |
|
386 throw new OutOfMemoryError("can't create offscreen surface"); |
|
387 } |
|
388 } |
|
389 |
|
390 /** |
|
391 * Creates a SurfaceData object representing the back buffer of a |
|
392 * double-buffered on-screen Window. |
|
393 */ |
|
394 /*public static CGLOffScreenSurfaceData createData(CPlatformView pView, |
|
395 Image image, int type) { |
|
396 CGLGraphicsConfig gc = getGC(pView); |
|
397 Rectangle r = pView.getBounds(); |
|
398 if (type == FLIP_BACKBUFFER) { |
|
399 return new CGLOffScreenSurfaceData(pView, gc, r.width, r.height, |
|
400 image, gc.getColorModel(), FLIP_BACKBUFFER); |
|
401 } else { |
|
402 return new CGLVSyncOffScreenSurfaceData(pView, gc, r.width, |
|
403 r.height, image, gc.getColorModel(), type); |
|
404 } |
|
405 }*/ |
|
406 |
|
407 /** |
|
408 * Creates a SurfaceData object representing an off-screen buffer (either a |
|
409 * FBO or Texture). |
|
410 */ |
|
411 public static MetalOffScreenSurfaceData createData(MetalGraphicsConfig gc, |
|
412 int width, int height, ColorModel cm, Image image, int type) { |
|
413 return new MetalOffScreenSurfaceData(null, gc, width, height, image, cm, |
|
414 type); |
|
415 } |
|
416 |
|
417 public static class MetalWindowSurfaceData extends MetalSurfaceData { |
|
418 |
|
419 public MetalWindowSurfaceData(CPlatformView pView, |
|
420 MetalGraphicsConfig gc) { |
|
421 super(pView, gc, gc.getColorModel(), WINDOW, 0, 0); |
|
422 } |
|
423 |
|
424 @Override |
|
425 public SurfaceData getReplacement() { |
|
426 return pView.getSurfaceData(); |
|
427 } |
|
428 |
|
429 @Override |
|
430 public Rectangle getBounds() { |
|
431 Rectangle r = pView.getBounds(); |
|
432 return new Rectangle(0, 0, r.width, r.height); |
|
433 } |
|
434 |
|
435 /** |
|
436 * Returns destination Component associated with this SurfaceData. |
|
437 */ |
|
438 @Override |
|
439 public Object getDestination() { |
|
440 return pView.getDestination(); |
|
441 } |
|
442 |
|
443 @Override |
|
444 public void validate() { |
|
445 MetalRenderQueue rq = MetalRenderQueue.getInstance(); |
|
446 rq.lock(); |
|
447 try { |
|
448 rq.flushAndInvokeNow(new Runnable() { |
|
449 public void run() { |
|
450 Rectangle peerBounds = pView.getBounds(); |
|
451 validate(0, 0, peerBounds.width, peerBounds.height, pView.isOpaque()); |
|
452 } |
|
453 }); |
|
454 } finally { |
|
455 rq.unlock(); |
|
456 } |
|
457 } |
|
458 |
|
459 @Override |
|
460 public void invalidate() { |
|
461 super.invalidate(); |
|
462 clearWindow(); |
|
463 } |
|
464 } |
|
465 |
|
466 public static class MetalLayerSurfaceData extends MetalSurfaceData { |
|
467 |
|
468 private MetalLayer layer; |
|
469 |
|
470 public MetalLayerSurfaceData(MetalLayer layer, MetalGraphicsConfig gc, |
|
471 int width, int height) { |
|
472 super(layer, gc, gc.getColorModel(), RT_TEXTURE, width, height); |
|
473 this.layer = layer; |
|
474 initSurface(this.width, this.height); |
|
475 } |
|
476 |
|
477 @Override |
|
478 public SurfaceData getReplacement() { |
|
479 return layer.getSurfaceData(); |
|
480 } |
|
481 |
|
482 /*@Override |
|
483 boolean isOnScreen() { |
|
484 return true; |
|
485 }*/ |
|
486 |
|
487 @Override |
|
488 public Rectangle getBounds() { |
|
489 return new Rectangle(width, height); |
|
490 } |
|
491 |
|
492 @Override |
|
493 public Object getDestination() { |
|
494 return layer.getDestination(); |
|
495 } |
|
496 |
|
497 @Override |
|
498 public int getTransparency() { |
|
499 return layer.getTransparency(); |
|
500 } |
|
501 |
|
502 @Override |
|
503 public void invalidate() { |
|
504 super.invalidate(); |
|
505 clearWindow(); |
|
506 } |
|
507 } |
|
508 |
|
509 public static class MetalOffScreenSurfaceData extends MetalSurfaceData { |
|
510 private Image offscreenImage; |
|
511 |
|
512 public MetalOffScreenSurfaceData(CPlatformView pView, |
|
513 MetalGraphicsConfig gc, int width, int height, Image image, |
|
514 ColorModel cm, int type) { |
|
515 super(pView, gc, cm, type, width, height); |
|
516 offscreenImage = image; |
|
517 initSurface(this.width, this.height); |
|
518 } |
|
519 |
|
520 @Override |
|
521 public SurfaceData getReplacement() { |
|
522 return restoreContents(offscreenImage); |
|
523 } |
|
524 |
|
525 @Override |
|
526 public Rectangle getBounds() { |
|
527 if (type == FLIP_BACKBUFFER) { |
|
528 Rectangle r = pView.getBounds(); |
|
529 return new Rectangle(0, 0, r.width, r.height); |
|
530 } else { |
|
531 return new Rectangle(width, height); |
|
532 } |
|
533 } |
|
534 |
|
535 /** |
|
536 * Returns destination Image associated with this SurfaceData. |
|
537 */ |
|
538 @Override |
|
539 public Object getDestination() { |
|
540 return offscreenImage; |
|
541 } |
|
542 } |
|
543 // TODO : We have some OGL Mac specific functions, verify their use case |
|
544 } |