1 /*
   2  * Copyright (c) 2011, 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 #import "AWTSurfaceLayers.h"
  27 #import "ThreadUtilities.h"
  28 #import "LWCToolkit.h"
  29 
  30 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  31 
  32 @implementation AWTSurfaceLayers
  33 
  34 @synthesize windowLayer;
  35 
  36 - (id) initWithWindowLayer:(CALayer *)aWindowLayer {
  37     self = [super init];
  38     if (self == nil) return self;
  39 
  40     windowLayer = aWindowLayer;
  41 
  42     return self;
  43 }
  44 
  45 
  46 - (CALayer *) layer {
  47     return layer;
  48 }
  49 
  50 - (void) setLayer:(CALayer *)newLayer {
  51     if (layer != newLayer) {
  52         if (layer != nil || newLayer == nil) {
  53             [layer removeFromSuperlayer];
  54             [layer release];
  55         }
  56 
  57         if (newLayer != nil) {
  58             layer = [newLayer retain];
  59             // REMIND: window layer -> container layer
  60             [windowLayer addSublayer: layer];
  61         }
  62     }
  63 }
  64 
  65 // Updates back buffer size of the layer if it's an OpenGL layer
  66 // including all OpenGL sublayers
  67 + (void) repaintLayersRecursively:(CALayer*)aLayer {
  68     if ([aLayer isKindOfClass:[CAOpenGLLayer class]]) {
  69         [aLayer setNeedsDisplay];
  70     }
  71     for(CALayer *child in aLayer.sublayers) {
  72         [AWTSurfaceLayers repaintLayersRecursively: child];
  73     }
  74 }
  75 
  76 - (void) setBounds:(CGRect)rect {
  77     layer.anchorPoint = CGPointMake(0, 0);
  78 
  79     // translates values to the coordinate system of the "root" layer
  80     CGFloat newY = windowLayer.bounds.size.height - rect.origin.y - rect.size.height;
  81 
  82     // REMIND: why do we need to inverse position?
  83     CGRect newRect = CGRectMake(-rect.origin.x, -newY, rect.size.width, rect.size.height);
  84 
  85     layer.bounds = newRect;
  86     [AWTSurfaceLayers repaintLayersRecursively:layer];
  87 }
  88 
  89 @end
  90 
  91 /*
  92  * Class:     sun_lwawt_macosx_CPlatformComponent
  93  * Method:    nativeCreateLayer
  94  * Signature: ()J
  95  */
  96 JNIEXPORT jlong JNICALL
  97 Java_sun_lwawt_macosx_CPlatformComponent_nativeCreateComponent
  98 (JNIEnv *env, jobject obj, jlong windowLayerPtr)
  99 {
 100   __block AWTSurfaceLayers *surfaceLayers = nil;
 101 
 102 JNF_COCOA_ENTER(env);
 103 AWT_ASSERT_NOT_APPKIT_THREAD;
 104 
 105   [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
 106       AWT_ASSERT_APPKIT_THREAD;
 107 
 108       CALayer *windowLayer = jlong_to_ptr(windowLayerPtr);
 109       surfaceLayers = [[AWTSurfaceLayers alloc] initWithWindowLayer: windowLayer];
 110       CFRetain(surfaceLayers);
 111       [surfaceLayers release];
 112     }];
 113 
 114 JNF_COCOA_EXIT(env);
 115 
 116   return ptr_to_jlong(surfaceLayers);
 117 }
 118 
 119 /*
 120  * Class:     sun_lwawt_macosx_CPlatformComponent
 121  * Method:    nativeSetBounds
 122  * Signature: (JIIII)V
 123  */
 124 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformComponent_nativeSetBounds
 125 (JNIEnv *env, jclass clazz, jlong surfaceLayersPtr, jint x, jint y, jint width, jint height)
 126 {
 127 JNF_COCOA_ENTER(env);
 128 
 129   AWTSurfaceLayers *surfaceLayers = OBJC(surfaceLayersPtr);
 130   [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 131       AWT_ASSERT_APPKIT_THREAD;
 132 
 133       CGRect rect = CGRectMake(x, y, width, height);
 134       [surfaceLayers setBounds: rect];
 135     }];
 136 
 137 JNF_COCOA_EXIT(env);
 138 }