changeset 57853:3108e425da7f

8233231: Implement PaintType : Texture Paint This is basic untransformed texture paint support.
author aghaisas
date Mon, 03 Feb 2020 22:54:17 +0530
parents 70478031d1bb
children ac2aac2fd9db
files src/java.desktop/macosx/native/libawt_lwawt/awt/common.h src/java.desktop/macosx/native/libawt_lwawt/awt/shaders.metal src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLContext.m src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLPaints.h src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLPaints.m src/java.desktop/share/classes/sun/java2d/pipe/BufferedPaints.java
diffstat 6 files changed, 114 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/macosx/native/libawt_lwawt/awt/common.h	Fri Jan 31 13:39:17 2020 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/common.h	Mon Feb 03 22:54:17 2020 +0530
@@ -71,4 +71,10 @@
     int isSrcOpaque;
     int isDstOpaque;
 };
+
+struct AnchorData
+{
+    vector_float4 rect;
+    vector_float2 shear;
+};
 #endif
--- a/src/java.desktop/macosx/native/libawt_lwawt/awt/shaders.metal	Fri Jan 31 13:39:17 2020 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/shaders.metal	Mon Feb 03 22:54:17 2020 +0530
@@ -138,3 +138,35 @@
     return half4(c);
 }
 
+
+vertex TxtShaderInOut vert_tp(VertexInput in [[stage_in]],
+       constant AnchorData& anchorData [[buffer(FrameUniformBuffer)]],
+       constant TransformMatrix& transform [[buffer(MatrixBuffer)]]) {
+    TxtShaderInOut out;
+    float4 pos4 = float4(in.position, 0.0, 1.0);
+    out.position = transform.transformMatrix * pos4;
+
+    // Compute texture coordinates here w.r.t. anchor rect of texture paint
+    float window_width = 2.0 / transform.transformMatrix[0][0];
+    float window_height = -2.0 / transform.transformMatrix[1][1];
+
+    out.texCoords.x = ( ((1 + out.position.x) * (window_width/2.0)) - anchorData.rect[0]) / (anchorData.rect[2]);
+    out.texCoords.y = ( ((1 - out.position.y) * (window_height/2.0)) - anchorData.rect[1]) / (anchorData.rect[3]);
+  
+    return out;
+}
+
+fragment half4 frag_tp(
+        TxtShaderInOut vert [[stage_in]],
+        texture2d<float, access::sample> renderTexture [[texture(0)]],
+        constant TxtFrameUniforms& uniforms [[buffer(1)]]
+        )
+{
+    constexpr sampler textureSampler (address::repeat,
+                                      mag_filter::nearest,
+                                      min_filter::nearest);
+
+    float4 pixelColor = renderTexture.sample(textureSampler, vert.texCoords);
+    float srcA = uniforms.isSrcOpaque ? 1 : pixelColor.a;
+    return half4(pixelColor.r, pixelColor.g, pixelColor.b, srcA);
+}
--- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLContext.m	Fri Jan 31 13:39:17 2020 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLContext.m	Mon Feb 03 22:54:17 2020 +0530
@@ -385,8 +385,16 @@
                     yp3:(jdouble)yp3
 {
     J2dTraceLn(J2D_TRACE_INFO, "MTLContext.setTexturePaint");
+
+    BMTLSDOps *srcOps = (BMTLSDOps *)jlong_to_ptr(pSrcOps);
+    
+    if (srcOps == NULL || srcOps->pTexture == NULL) {
+        J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLContext_setTexturePaint: texture paint - texture is null");
+        return;
+    }
+
     [_paint setTexture:useMask
-               pSrcOps:pSrcOps
+               textureID:srcOps->pTexture
                 filter:filter
                    xp0:xp0
                    xp1:xp1
--- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLPaints.h	Fri Jan 31 13:39:17 2020 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLPaints.h	Mon Feb 03 22:54:17 2020 +0530
@@ -82,7 +82,7 @@
                    pixels:(void *)pixels;
 
 - (void)setTexture:(jboolean)useMask
-           pSrcOps:(jlong)pSrcOps
+           textureID:(id<MTLTexture>)textureID
             filter:(jboolean)filter
                xp0:(jdouble)xp0
                xp1:(jdouble)xp1
--- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLPaints.m	Fri Jan 31 13:39:17 2020 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLPaints.m	Mon Feb 03 22:54:17 2020 +0530
@@ -102,6 +102,10 @@
     jint          _pixel1;
     jint          _pixel2;
     jboolean      _useMask;
+
+    // texture paint
+    id<MTLTexture> _paintTexture;
+    struct AnchorData _anchor;
 }
 
 - (id)init {
@@ -129,6 +133,14 @@
     if (_paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR) {
         return _color == other->_color;
     }
+    if (_paintState == sun_java2d_SunGraphics2D_PAINT_TEXTURE) {
+        return _paintTexture == other->_paintTexture &&
+              _anchor.rect[0] == other->_anchor.rect[0] &&
+              _anchor.rect[1] == other->_anchor.rect[1] &&
+              _anchor.rect[2] == other->_anchor.rect[2] &&
+              _anchor.rect[3] == other->_anchor.rect[3];
+    }
+
     J2dTraceLn1(J2D_TRACE_ERROR, "Unimplemented paint mode %d", _paintState);
     return NO;
 }
@@ -150,19 +162,36 @@
         _color == other->_color;
         return;
     }
+
+    if (_paintState == sun_java2d_SunGraphics2D_PAINT_TEXTURE) {
+        _color == other->_color;
+        _paintTexture = other->_paintTexture;
+        _anchor = other->_anchor;
+        return;
+    }
+
     J2dTraceLn1(J2D_TRACE_ERROR, "Unsupported paint mode %d", _paintState);
 }
 
 - (NSString *)getDescription {
-    if (_paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR)
+    if (_paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR) {
         return [NSString stringWithFormat:@"[r=%d g=%d b=%d a=%d]", (_color >> 16) & (0xFF), (_color >> 8) & 0xFF, (_color) & 0xFF, (_color >> 24) & 0xFF];
-    if (_paintState == sun_java2d_SunGraphics2D_PAINT_GRADIENT)
+    }
+    
+    if (_paintState == sun_java2d_SunGraphics2D_PAINT_GRADIENT) {
         return [NSString stringWithFormat:@"gradient"];
+    }
+
+    if (_paintState == sun_java2d_SunGraphics2D_PAINT_TEXTURE) {
+        return [NSString stringWithFormat:@"texture_paint"];
+    }
+
     return @"unknown-paint";
 }
 
 - (void)reset {
     _paintState = sun_java2d_SunGraphics2D_PAINT_UNDEFINED;
+    _paintTexture = nil;
 }
 
 - (void)setColor:(jint)pixelColor {
@@ -229,7 +258,7 @@
 }
 
 - (void)setTexture:(jboolean)useMask
-           pSrcOps:(jlong)pSrcOps
+           textureID:(id<MTLTexture>)textureID
             filter:(jboolean)filter
                xp0:(jdouble)xp0
                xp1:(jdouble)xp1
@@ -238,8 +267,16 @@
                yp1:(jdouble)yp1
                yp3:(jdouble)yp3
 {
-    J2dTraceLn(J2D_TRACE_ERROR, "setTexture: UNIMPLEMENTED");
-    [self setColor:0];
+    _paintState = sun_java2d_SunGraphics2D_PAINT_TEXTURE;
+    _paintTexture = textureID;
+
+    _anchor.rect[0] = xp3;
+    _anchor.rect[1] = yp3;
+    _anchor.rect[2] = xp0;
+    _anchor.rect[3] = yp1;
+
+    //_anchor.shear[0] = xp1;
+    //_anchor.shear[1] = yp0;
 }
 
 // For the current paint mode:
@@ -320,6 +357,18 @@
                     RGBA_TO_V4(_pixel1),
                     RGBA_TO_V4(_pixel2)};
             [encoder setFragmentBytes: &uf length:sizeof(uf) atIndex:0];
+        } else if (_paintState == sun_java2d_SunGraphics2D_PAINT_TEXTURE) {
+            pipelineState = [pipelineStateStorage getPipelineState:templateRenderPipelineDesc
+                                        vertexShaderId:@"vert_tp"
+                                      fragmentShaderId:@"frag_tp"
+                                         compositeRule:[composite getRule]
+                                                  isAA:isAA
+                                              srcFlags:srcFlags
+                                              dstFlags:dstFlags
+                                         stencilNeeded:stencil];
+
+            [encoder setVertexBytes:&_anchor length:sizeof(_anchor) atIndex:FrameUniformBuffer];
+            [encoder setFragmentTexture:_paintTexture atIndex: 0];
         }
     }
 
--- a/src/java.desktop/share/classes/sun/java2d/pipe/BufferedPaints.java	Fri Jan 31 13:39:17 2020 +0530
+++ b/src/java.desktop/share/classes/sun/java2d/pipe/BufferedPaints.java	Mon Feb 03 22:54:17 2020 +0530
@@ -44,6 +44,7 @@
 import sun.java2d.SurfaceData;
 import sun.java2d.loops.CompositeType;
 import sun.java2d.loops.SurfaceType;
+import sun.java2d.macos.MacOSFlags;
 import static sun.java2d.pipe.BufferedOpCodes.*;
 
 import java.lang.annotation.Native;
@@ -263,13 +264,17 @@
 
         double xp0, xp1, xp3, yp0, yp1, yp3;
         try {
-            at.invert();
-            xp0 = at.getScaleX();
-            xp1 = at.getShearX();
-            xp3 = at.getTranslateX();
-            yp0 = at.getShearY();
-            yp1 = at.getScaleY();
-            yp3 = at.getTranslateY();
+                // Do not invert the matrix for metal pipeline
+                if (MacOSFlags.isMetalEnabled() == false) {
+                   at.invert();
+                }
+
+                xp0 = at.getScaleX();
+                xp1 = at.getShearX();
+                xp3 = at.getTranslateX();
+                yp0 = at.getShearY();
+                yp1 = at.getScaleY();
+                yp3 = at.getTranslateY();
         } catch (java.awt.geom.NoninvertibleTransformException e) {
             xp0 = xp1 = xp3 = yp0 = yp1 = yp3 = 0.0;
         }