1 /*
   2  * Copyright (c) 2012, 2013, 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 #include "LensCommon.h"
  27 #include "com_sun_glass_ui_lens_LensView.h"
  28 #include "wm/LensWindowManager.h"
  29 
  30 LensResult glass_view_releaseNativeView(JNIEnv *env, NativeView view) {
  31 
  32     GLASS_LOG_FINE("releaseNativeView on view %p", view);
  33 
  34     glass_view_PlatformViewRelease(env, view);
  35 
  36     GLASS_LOG_FINE("Releasing LensView global reference for view (%p)", view);
  37     (*env)->DeleteGlobalRef(env, view->lensView);
  38 
  39     GLASS_LOG_FINE("freeing view (%p)", view);
  40     free(view);
  41 
  42     return LENS_OK;
  43 }
  44 
  45 void glass_view_fitSurfaceToScreen(NativeScreen screen,
  46                                    NativeView view) {
  47 
  48     /**
  49     * Calculating the new diminsions is done by comparing the
  50     * vertical and the horizontal ratios, between the screen and
  51     * the view, and using the smaller ratio of the two to
  52     * calculate the surface size(the window must occupy the whole
  53     * screen).
  54     * 0.5 is added to the calculation for rounding conversion (so
  55     * 3.7 will become 4 when converted to int)
  56     *
  57     * Example:
  58     * Screen is 1200 X 700, view is 356 X 321
  59     * 1. width ratio = 1200/356 = 3.370
  60     * 2. height ratio = 700/321 = 2.180
  61     * 3. 2.180 is smaller, so we use it
  62     * 4. new width = (int)356*2.180+0.5 = 776
  63     * 5. new height = (int) 321*2.180+0.5 = 700
  64     * 6. new ratio = 776/700 = 1.1 - old ratio = 356/321 = 1.1
  65     *
  66     */
  67 
  68     float wRatio = screen->width / view->bounds.width;
  69     float hRatio = screen->height / view->bounds.height;
  70     float ratio  = (wRatio < hRatio) ? wRatio : hRatio;
  71 
  72     GLASS_LOG_FINE("Got screen->width=%d, screen->height=%d "
  73                    "view->width=%d, ciew->height=%d",
  74                    screen->width, screen->height,
  75                    view->bounds.width, view->bounds.height);
  76 
  77     view->bounds.width  = (int)(screen->width * ratio + 0.5);
  78     view->bounds.height = (int)(screen->height * ratio + 0.5);
  79 
  80     //center the surface on the screen
  81     view->bounds.x = (screen->width - view->bounds.width) / 2;
  82     view->bounds.y = (screen->height - view->bounds.height) / 2;
  83 
  84     GLASS_LOG_FINE("New bounds are width=%d, height=%d, x=%, y=%d, "
  85                    "used ratio is %f",
  86                    view->bounds.width, view->bounds.height,
  87                    view->bounds.x, view->bounds.y, ratio);
  88 
  89 }
  90 
  91 
  92 /*
  93  * Class:     com_sun_glass_ui_lens_LensView
  94  * Method:    _createNativeView
  95  * Signature: (Ljava/util/Map;)J
  96  */
  97 JNIEXPORT jlong JNICALL Java_com_sun_glass_ui_lens_LensView__1createNativeView
  98 (JNIEnv *env, jobject lensView, jobject MapObject) {
  99 
 100     NativeView view = (NativeView)calloc(1, sizeof(struct _NativeView));
 101     if (view != NULL) {
 102         view->lensView = (*env)->NewGlobalRef(env, lensView);
 103 
 104         GLASS_LOG_FINE("Created NativeView view = %p lensView=%p",
 105                        view, view->lensView);
 106 
 107         view->parent = NULL;
 108 
 109         if (glass_view_PlatformViewData_create(view)) {
 110             GLASS_LOG_SEVERE("Failed to init platform view");
 111             glass_view_releaseNativeView(env , view);
 112             view = NULL;
 113         }
 114     } else {
 115         GLASS_LOG_SEVERE("Failed to allocate NativeView");
 116         return 0;
 117     }
 118     return ptr_to_jlong(view);
 119 }
 120 
 121 /*
 122  * Class:     com_sun_glass_ui_lens_LensView
 123  * Method:    _begin
 124  * Signature: (JZ)V
 125  */
 126 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensView__1begin
 127 (JNIEnv *env, jobject ViewObject, jlong nativeViewPtr) {
 128 
 129     NativeView view = (NativeView) jlong_to_ptr(nativeViewPtr);
 130     if (view == NULL) {
 131         glass_throw_exception_by_name(
 132             env, glass_NullPointerException, "View handle is null");
 133     }
 134 
 135     glass_view_drawBegin(view);
 136 }
 137 
 138 /*
 139  * Class:     com_sun_glass_ui_lens_LensView
 140  * Method:    _end
 141  * Signature: (JZZ)V
 142  */
 143 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensView__1end
 144 (JNIEnv *env , jobject ViewObject, jlong nativeViewPtr) {
 145 
 146     NativeView view = (NativeView) jlong_to_ptr(nativeViewPtr);
 147     if (view == NULL) {
 148         glass_throw_exception_by_name(
 149             env, glass_NullPointerException, "View handle is null");
 150     }
 151 
 152     glass_view_drawEnd(view);
 153 }
 154 
 155 
 156 /*
 157  * Class:     com_sun_glass_ui_lens_LensView
 158  * Method:    _paintIntBuffer
 159  * Signature: (JIILjava/nio/IntBuffer;[II)V
 160  */
 161 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensView__1paintInt(JNIEnv *env,
 162         jobject ViewObject,
 163         jlong nativeViewPtr,
 164         jint width,
 165         jint height,
 166         jobject intBuffer,
 167         jintArray srcArray,
 168         jint offset) {
 169 
 170     NativeView view = (NativeView)jlong_to_ptr(nativeViewPtr);
 171     NativeWindow window = view->parent;
 172     if (window == NULL) {
 173         glass_throw_exception_by_name(
 174             env, glass_NullPointerException, "Window handle is null paintint");
 175     }
 176 
 177     jint *src = (*env)->GetPrimitiveArrayCritical(env, srcArray, 0);
 178 
 179     glass_pixel_attachIntBuffer(env,
 180                                 src,
 181                                 window,
 182                                 width, height,  offset);
 183     lens_wm_notifyWindowUpdate(window, width, height);
 184 
 185     (*env)->ReleasePrimitiveArrayCritical(env, srcArray, src, JNI_ABORT);
 186 }
 187 
 188 
 189 /*
 190  * Class:     com_sun_glass_ui_lens_LensView
 191  * Method:    _paintByte
 192  * Signature: (JIILjava/nio/ByteBuffer;[BI)V
 193  */
 194 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensView__1paintByte
 195 (JNIEnv *env, jobject ViewObject, jlong nativeViewPtr, jint width,
 196  jint height, jobject bytes, jbyteArray array, jint offset) {
 197     glass_throw_exception_by_name(env, glass_RuntimeException, "Unimplemented");
 198 }
 199 
 200 /*
 201  * Class:     com_sun_glass_ui_lens_LensView
 202  * Method:    _paintIntDirect
 203  * Signature: (JIILjava/nio/Buffer;)V
 204  */
 205 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensView__1paintIntDirect
 206 (JNIEnv *env, jobject ViewObject, jlong nativeViewPtr, jint width,
 207  jint height, jobject intBuffer) {
 208 
 209     NativeView view = (NativeView)jlong_to_ptr(nativeViewPtr);
 210     NativeWindow window = view->parent;
 211     if (window == NULL) {
 212         glass_throw_exception_by_name(
 213             env, glass_NullPointerException, "Window handle is null");
 214     }
 215 
 216     jint *src = (*env)->GetDirectBufferAddress(env, intBuffer);
 217     int offset = 0;
 218 
 219     if (src) {
 220         glass_pixel_attachIntBuffer(env,
 221                                     src,
 222                                     window,
 223                                     width, height,  offset);
 224         lens_wm_notifyWindowUpdate(window, width, height);
 225     }
 226 }
 227 
 228 /*
 229  * Class:     com_sun_glass_ui_lens_LensView
 230  * Method:    _close
 231  * Signature: (J)Z
 232  */
 233 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_lens_LensView__1close
 234 (JNIEnv *env , jobject ViewObject, jlong nativeViewPtr) {
 235 
 236     //It seems that there is no need for view.close() notification
 237     NativeView view = (NativeView) jlong_to_ptr(nativeViewPtr);
 238     if (view == NULL) {
 239         glass_throw_exception_by_name(
 240             env, glass_NullPointerException, "View handle is null");
 241     }
 242 
 243     int result;
 244 
 245     GLASS_LOG_FINE("close view %p", view);
 246     result = glass_view_releaseNativeView(env , view);
 247     if (result != LENS_OK) {
 248         GLASS_LOG_SEVERE("Failed to close a native view");
 249     }
 250 
 251     return (result ? JNI_FALSE : JNI_TRUE);
 252 }
 253 
 254 /*
 255  * Class:     com_sun_glass_ui_lens_LensView
 256  * Method:    _enterFullscreen
 257  * Signature: (JZZZ)Z
 258  */
 259 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_lens_LensView__1enterFullscreen
 260 (JNIEnv *env, jobject ViewObject, jlong nativeViewPtr, jboolean animate,
 261  jboolean keepRatio, jboolean hideCursor) {
 262 
 263     NativeView view = (NativeView) jlong_to_ptr(nativeViewPtr);
 264     if (view == NULL) {
 265         glass_throw_exception_by_name(
 266             env, glass_NullPointerException, "View handle is null");
 267     }
 268 
 269     GLASS_LOG_FINE(
 270         "enter fullscreen for view %p, animate=%s, keepRatio=%s, hideCursor=%s",
 271         view,
 272         animate ? "true" : "false",
 273         keepRatio ? "true" : "false",
 274         hideCursor ? "true" : "false");
 275     return glass_view_enterFullscreen(env, view, animate, keepRatio,
 276                                       hideCursor);
 277 }
 278 
 279 /*
 280  * Class:     com_sun_glass_ui_lens_LensView
 281  * Method:    _exitFullscreen
 282  * Signature: (JZ)V
 283  */
 284 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensView__1exitFullscreen
 285 (JNIEnv *env, jobject ViewObject, jlong nativeViewPtr, jboolean animate) {
 286 
 287     NativeView view = (NativeView) jlong_to_ptr(nativeViewPtr);
 288     if (view == NULL) {
 289         glass_throw_exception_by_name(
 290             env, glass_NullPointerException, "View handle is null");
 291     }
 292 
 293     GLASS_LOG_FINE(
 294         "exit fullscreen for view %p", view);
 295     (void) glass_view_exitFullscreen(env, view, animate);
 296 }
 297 
 298 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensView__1setParent
 299 (JNIEnv *env, jobject _this, jlong nativeViewPtr, jlong nativeWindowPtr) {
 300 
 301 
 302     NativeView view = (NativeView) jlong_to_ptr(nativeViewPtr);
 303     if (view == NULL) {
 304         glass_throw_exception_by_name(
 305             env, glass_NullPointerException, "View handle is null");
 306     }
 307 
 308     NativeWindow parent = (NativeWindow) jlong_to_ptr(nativeWindowPtr);
 309     // window can be NULL if being removed
 310 
 311     GLASS_LOG_FINE("set parent of view %p to window %d[%p] old window %d[%p]",
 312                    view,
 313                    parent?parent->id:-1,
 314                    parent,
 315                    view->parent?view->parent->id:-1,
 316                    view->parent);
 317     glass_view_setParent(env, parent, view);
 318 }