|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +#import "JAVAVM/jawt_md.h" |
| 25 | +#import <Quartz/Quartz.h> |
| 26 | + |
| 27 | +/* |
| 28 | + * Pass the block to a selector of a class that extends NSObject |
| 29 | + * There is no need to copy the block since this class always waits. |
| 30 | + */ |
| 31 | +@interface BlockRunner : NSObject { } |
| 32 | + |
| 33 | ++ (void)invokeBlock:(void (^)())block; |
| 34 | +@end |
| 35 | + |
| 36 | +@implementation BlockRunner |
| 37 | + |
| 38 | ++ (void)invokeBlock:(void (^)())block{ |
| 39 | + block(); |
| 40 | +} |
| 41 | + |
| 42 | ++ (void)performBlock:(void (^)())block { |
| 43 | + [self performSelectorOnMainThread:@selector(invokeBlock:) withObject:block waitUntilDone:YES]; |
| 44 | +} |
| 45 | + |
| 46 | +@end |
| 47 | + |
| 48 | + |
| 49 | +/* |
| 50 | + * Class: MyMacCanvas |
| 51 | + * Method: paint |
| 52 | + * SIgnature: (Ljava/awt/Graphics;)V |
| 53 | + */ |
| 54 | +JNIEXPORT void JNICALL Java_MyMacCanvas_addNativeCoreAnimationLayer |
| 55 | +(JNIEnv* env, jobject canvas, jobject graphics) |
| 56 | +{ |
| 57 | + printf("paint called\n"); |
| 58 | + |
| 59 | + JAWT awt; |
| 60 | + awt.version = JAWT_VERSION_1_4 | JAWT_MACOSX_USE_CALAYER; |
| 61 | + if (JAWT_GetAWT(env, &awt) == JNI_FALSE) { |
| 62 | + printf("AWT Not found\n"); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + printf("JAWT found\n"); |
| 67 | + |
| 68 | + /* Get the drawing surface */ |
| 69 | + JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, canvas); |
| 70 | + if (ds == NULL) { |
| 71 | + printf("NULL drawing surface\n"); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + /* Lock the drawing surface */ |
| 76 | + jint lock = ds->Lock(ds); |
| 77 | + printf("Lock value %d\n", (int)lock); |
| 78 | + if((lock & JAWT_LOCK_ERROR) != 0) { |
| 79 | + printf("Error locking surface"); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + /* Get the drawing surface info */ |
| 84 | + JAWT_DrawingSurfaceInfo *dsi = ds->GetDrawingSurfaceInfo(ds); |
| 85 | + if (dsi == NULL) { |
| 86 | + printf("Error getting surface info\n"); |
| 87 | + ds->Unlock(ds); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // create and attach the layer on the AppKit thread |
| 92 | + void (^block)() = ^(){ |
| 93 | + // attach the "root layer" to the AWT Canvas surface layers |
| 94 | + CALayer *layer = [[CALayer new] autorelease]; |
| 95 | + id <JAWT_SurfaceLayers> surfaceLayers = (id <JAWT_SurfaceLayers>)dsi->platformInfo; |
| 96 | + CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB (); |
| 97 | + CGFloat rgba[4] = {0.0, 0.0, 0.0, 1.0}; |
| 98 | + CGColorRef color = CGColorCreate (colorspace, rgba); |
| 99 | + layer.backgroundColor = color; |
| 100 | + surfaceLayers.layer = layer; |
| 101 | + }; |
| 102 | + |
| 103 | + if ([NSThread isMainThread]) { |
| 104 | + block(); |
| 105 | + } else { |
| 106 | + [BlockRunner performBlock:block]; |
| 107 | + } |
| 108 | + |
| 109 | + /* Free the drawing surface info */ |
| 110 | + ds->FreeDrawingSurfaceInfo(dsi); |
| 111 | + |
| 112 | + /* Unlock the drawing surface */ |
| 113 | + ds->Unlock(ds); |
| 114 | + |
| 115 | + /* Free the drawing surface */ |
| 116 | + awt.FreeDrawingSurface(ds); |
| 117 | +} |
0 commit comments