< prev index next >

src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLLayer.m

Print this page

 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 #import "MTLGraphicsConfig.h"
 27 #import "MTLLayer.h"
 28 #import "ThreadUtilities.h"
 29 #import "LWCToolkit.h"
 30 #import "MTLSurfaceData.h"
 31 
 32 #import "MTLBlitLoops.h"
 33 
 34 @implementation MTLLayer
 35 
 36 
 37 @synthesize javaLayer;
 38 @synthesize ctx;
 39 @synthesize bufferWidth;
 40 @synthesize bufferHeight;
 41 @synthesize buffer;
 42 @synthesize nextDrawableCount;


 43 @synthesize topInset;
 44 @synthesize leftInset;
 45 
 46 - (id) initWithJavaLayer:(JNFWeakJObjectWrapper *)layer
 47 {
 48     AWT_ASSERT_APPKIT_THREAD;
 49     // Initialize ourselves
 50     self = [super init];
 51     if (self == nil) return self;
 52 
 53     self.javaLayer = layer;
 54 
 55     self.contentsGravity = kCAGravityTopLeft;
 56 
 57     //Disable CALayer's default animation
 58     NSMutableDictionary * actions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
 59                                     [NSNull null], @"anchorPoint",
 60                                     [NSNull null], @"bounds",
 61                                     [NSNull null], @"contents",
 62                                     [NSNull null], @"contentsScale",
 63                                     [NSNull null], @"onOrderIn",
 64                                     [NSNull null], @"onOrderOut",
 65                                     [NSNull null], @"position",
 66                                     [NSNull null], @"sublayers",
 67                                     nil];
 68     self.actions = actions;
 69     [actions release];
 70     self.topInset = 0;
 71     self.leftInset = 0;
 72     self.framebufferOnly = NO;
 73     self.nextDrawableCount = 0;
 74     return self;
 75 }
 76 
 77 - (void) blitTexture {
 78     if (self.ctx == NULL || self.javaLayer == NULL || self.buffer == nil || self.ctx.device == nil) {
 79         J2dTraceLn4(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: uninitialized (mtlc=%p, javaLayer=%p, buffer=%p, devide=%p)", self.ctx, self.javaLayer, self.buffer, ctx.device);
 80         return;
 81     }
 82 
 83     if (self.nextDrawableCount != 0)
 84         return;
 85 
 86     @autoreleasepool {
 87         if ((self.buffer.width == 0) || (self.buffer.height == 0)) {
 88             J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: cannot create drawable of size 0");
 89             return;
 90         }
 91 
 92         id<MTLCommandBuffer> commandBuf = [self.ctx createBlitCommandBuffer];
 93         if (commandBuf == nil) {
 94             J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: nothing to do (commandBuf is null)");
 95             return;
 96         }
 97 
 98         id<CAMetalDrawable> mtlDrawable = [self nextDrawable];
 99         if (mtlDrawable == nil) {
100             J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: nextDrawable is null)");
101             return;
102         }
103         self.nextDrawableCount++;
104         J2dTraceLn6(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: src tex=%p (w=%d, h=%d), dst tex=%p (w=%d, h=%d)", self.buffer, self.buffer.width, self.buffer.height, mtlDrawable.texture, mtlDrawable.texture.width, mtlDrawable.texture.height);
105         id <MTLBlitCommandEncoder> blitEncoder = [commandBuf blitCommandEncoder];
106         [blitEncoder
107                 copyFromTexture:self.buffer sourceSlice:0 sourceLevel:0
108                 sourceOrigin:MTLOriginMake((jint)(self.leftInset*self.contentsScale), (jint)(self.topInset*self.contentsScale), 0)
109                 sourceSize:MTLSizeMake(self.buffer.width, self.buffer.height, 1)
110                 toTexture:mtlDrawable.texture destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(0, 0, 0)];
111         [blitEncoder endEncoding];
112 
113         [commandBuf presentDrawable:mtlDrawable];
114 
115         [commandBuf addCompletedHandler:^(id <MTLCommandBuffer> commandBuf) {
116             self.nextDrawableCount--;
117         }];
118 
119         [commandBuf commit];
120     }
121 }
122 
123 - (void) dealloc {
124     self.javaLayer = nil;
125     [super dealloc];
126 }
127 
128 - (void) blitCallback {
129 
130     JNIEnv *env = [ThreadUtilities getJNIEnv];
131     static JNF_CLASS_CACHE(jc_JavaLayer, "sun/java2d/metal/MTLLayer");
132     static JNF_MEMBER_CACHE(jm_drawInMTLContext, jc_JavaLayer, "drawInMTLContext", "()V");
133 
134     jobject javaLayerLocalRef = [self.javaLayer jObjectWithEnv:env];
135     if ((*env)->IsSameObject(env, javaLayerLocalRef, NULL)) {
136         return;
137     }
138 
139     JNFCallVoidMethod(env, javaLayerLocalRef, jm_drawInMTLContext);
140     (*env)->DeleteLocalRef(env, javaLayerLocalRef);
141 }
142 






























143 - (void) display {
144     AWT_ASSERT_APPKIT_THREAD;
145     J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer_display() called");

146     [self blitCallback];
147     [super display];
148 }
149 @end
150 
151 /*
152  * Class:     sun_java2d_metal_MTLLayer
153  * Method:    nativeCreateLayer
154  * Signature: ()J
155  */
156 JNIEXPORT jlong JNICALL
157 Java_sun_java2d_metal_MTLLayer_nativeCreateLayer
158 (JNIEnv *env, jobject obj)
159 {
160     __block MTLLayer *layer = nil;
161 
162 JNF_COCOA_ENTER(env);
163 
164     JNFWeakJObjectWrapper *javaLayer = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env];
165 

 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 #import "MTLGraphicsConfig.h"
 27 #import "MTLLayer.h"
 28 #import "ThreadUtilities.h"
 29 #import "LWCToolkit.h"
 30 #import "MTLSurfaceData.h"
 31 
 32 #import "MTLBlitLoops.h"
 33 
 34 @implementation MTLLayer
 35 
 36 
 37 @synthesize javaLayer;
 38 @synthesize ctx;
 39 @synthesize bufferWidth;
 40 @synthesize bufferHeight;
 41 @synthesize buffer;
 42 @synthesize mtlDrawable;
 43 @synthesize blitCommandBuf;
 44 @synthesize blitEncoder;
 45 @synthesize topInset;
 46 @synthesize leftInset;
 47 
 48 - (id) initWithJavaLayer:(JNFWeakJObjectWrapper *)layer
 49 {
 50     AWT_ASSERT_APPKIT_THREAD;
 51     // Initialize ourselves
 52     self = [super init];
 53     if (self == nil) return self;
 54 
 55     self.javaLayer = layer;
 56 
 57     self.contentsGravity = kCAGravityTopLeft;
 58 
 59     //Disable CALayer's default animation
 60     NSMutableDictionary * actions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
 61                                     [NSNull null], @"anchorPoint",
 62                                     [NSNull null], @"bounds",
 63                                     [NSNull null], @"contents",
 64                                     [NSNull null], @"contentsScale",
 65                                     [NSNull null], @"onOrderIn",
 66                                     [NSNull null], @"onOrderOut",
 67                                     [NSNull null], @"position",
 68                                     [NSNull null], @"sublayers",
 69                                     nil];
 70     self.actions = actions;
 71     [actions release];
 72     self.topInset = 0;
 73     self.leftInset = 0;
 74     self.framebufferOnly = NO;

 75     return self;
 76 }
 77 
 78 - (void) blitTexture {








 79     @autoreleasepool {
 80         [self.blitEncoder



















 81                 copyFromTexture:self.buffer sourceSlice:0 sourceLevel:0
 82                 sourceOrigin:MTLOriginMake((jint)(self.leftInset*self.contentsScale), (jint)(self.topInset*self.contentsScale), 0)
 83                 sourceSize:MTLSizeMake(self.buffer.width, self.buffer.height, 1)
 84                 toTexture:self.mtlDrawable.texture destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(0, 0, 0)];
 85         [self.blitEncoder endEncoding];
 86 
 87         [self.blitCommandBuf presentDrawable:self.mtlDrawable];
 88 
 89         [self.blitCommandBuf commit];




 90     }
 91 }
 92 
 93 - (void) dealloc {
 94     self.javaLayer = nil;
 95     [super dealloc];
 96 }
 97 
 98 - (void) blitCallback {
 99 
100     JNIEnv *env = [ThreadUtilities getJNIEnv];
101     static JNF_CLASS_CACHE(jc_JavaLayer, "sun/java2d/metal/MTLLayer");
102     static JNF_MEMBER_CACHE(jm_drawInMTLContext, jc_JavaLayer, "drawInMTLContext", "()V");
103 
104     jobject javaLayerLocalRef = [self.javaLayer jObjectWithEnv:env];
105     if ((*env)->IsSameObject(env, javaLayerLocalRef, NULL)) {
106         return;
107     }
108 
109     JNFCallVoidMethod(env, javaLayerLocalRef, jm_drawInMTLContext);
110     (*env)->DeleteLocalRef(env, javaLayerLocalRef);
111 }
112 
113 - (void) initBlit {
114     if (self.ctx == NULL || self.javaLayer == NULL || self.buffer == nil || self.ctx.device == nil) {
115         J2dTraceLn4(J2D_TRACE_VERBOSE, "MTLLayer.initBlit: uninitialized (mtlc=%p, javaLayer=%p, buffer=%p, devide=%p)", self.ctx, self.javaLayer, self.buffer, ctx.device);
116         return;
117     }
118 
119     if ((self.buffer.width == 0) || (self.buffer.height == 0)) {
120         J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.initBlit: cannot create drawable of size 0");
121         return;
122     }
123     self.blitCommandBuf = [self.ctx createBlitCommandBuffer];
124     if (self.blitCommandBuf == nil) {
125         J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.initBlit: nothing to do (commandBuf is null)");
126         return;
127     }
128 
129     self.blitEncoder = [self.blitCommandBuf blitCommandEncoder];
130     if (self.blitEncoder == nil) {
131         J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.initBlit: blitEncoder is null)");
132         return;
133     }
134 
135     self.mtlDrawable = [self nextDrawable];
136     if (self.mtlDrawable == nil) {
137         J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.initBlit: nextDrawable is null)");
138         return;
139     }
140     J2dTraceLn6(J2D_TRACE_VERBOSE, "MTLLayer.initBlit: src tex=%p (w=%d, h=%d), dst tex=%p (w=%d, h=%d)", self.buffer, self.buffer.width, self.buffer.height, self.mtlDrawable.texture, self.mtlDrawable.texture.width, self.mtlDrawable.texture.height);
141 }
142 
143 - (void) display {
144     AWT_ASSERT_APPKIT_THREAD;
145     J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer_display() called");
146     [self initBlit];
147     [self blitCallback];
148     [super display];
149 }
150 @end
151 
152 /*
153  * Class:     sun_java2d_metal_MTLLayer
154  * Method:    nativeCreateLayer
155  * Signature: ()J
156  */
157 JNIEXPORT jlong JNICALL
158 Java_sun_java2d_metal_MTLLayer_nativeCreateLayer
159 (JNIEnv *env, jobject obj)
160 {
161     __block MTLLayer *layer = nil;
162 
163 JNF_COCOA_ENTER(env);
164 
165     JNFWeakJObjectWrapper *javaLayer = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env];
166 
< prev index next >