Path: blob/master/test/jdk/java/awt/JAWT/myfile.m
41149 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#import "JAVAVM/jawt_md.h"24#import <Quartz/Quartz.h>2526/*27* Pass the block to a selector of a class that extends NSObject28* There is no need to copy the block since this class always waits.29*/30@interface BlockRunner : NSObject { }3132+ (void)invokeBlock:(void (^)())block;33@end3435@implementation BlockRunner3637+ (void)invokeBlock:(void (^)())block{38block();39}4041+ (void)performBlock:(void (^)())block {42[self performSelectorOnMainThread:@selector(invokeBlock:) withObject:block waitUntilDone:YES];43}4445@end464748/*49* Class: MyMacCanvas50* Method: paint51* SIgnature: (Ljava/awt/Graphics;)V52*/53JNIEXPORT void JNICALL Java_MyMacCanvas_addNativeCoreAnimationLayer54(JNIEnv* env, jobject canvas, jobject graphics)55{56printf("paint called\n");5758JAWT awt;59awt.version = JAWT_VERSION_1_4 | JAWT_MACOSX_USE_CALAYER;60if (JAWT_GetAWT(env, &awt) == JNI_FALSE) {61printf("AWT Not found\n");62return;63}6465printf("JAWT found\n");6667/* Get the drawing surface */68JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, canvas);69if (ds == NULL) {70printf("NULL drawing surface\n");71return;72}7374/* Lock the drawing surface */75jint lock = ds->Lock(ds);76printf("Lock value %d\n", (int)lock);77if((lock & JAWT_LOCK_ERROR) != 0) {78printf("Error locking surface");79return;80}8182/* Get the drawing surface info */83JAWT_DrawingSurfaceInfo *dsi = ds->GetDrawingSurfaceInfo(ds);84if (dsi == NULL) {85printf("Error getting surface info\n");86ds->Unlock(ds);87return;88}8990// create and attach the layer on the AppKit thread91void (^block)() = ^(){92// attach the "root layer" to the AWT Canvas surface layers93CALayer *layer = [[CALayer new] autorelease];94id <JAWT_SurfaceLayers> surfaceLayers = (id <JAWT_SurfaceLayers>)dsi->platformInfo;95CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB ();96CGFloat rgba[4] = {0.0, 0.0, 0.0, 1.0};97CGColorRef color = CGColorCreate (colorspace, rgba);98layer.backgroundColor = color;99surfaceLayers.layer = layer;100};101102if ([NSThread isMainThread]) {103block();104} else {105[BlockRunner performBlock:block];106}107108/* Free the drawing surface info */109ds->FreeDrawingSurfaceInfo(dsi);110111/* Unlock the drawing surface */112ds->Unlock(ds);113114/* Free the drawing surface */115awt.FreeDrawingSurface(ds);116}117118119