1 /*
   2  * Copyright (c) 2014, 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 package com.sun.glass.ui.monocle;
  27 
  28 import com.sun.glass.ui.monocle.linux.LinuxSystem;
  29 
  30 public class AcceleratedScreen {
  31 
  32     private static long glesLibraryHandle;
  33     private static long eglLibraryHandle;
  34     protected static boolean initialized = false;
  35     long eglSurface, eglContext, eglDisplay;
  36 
  37     protected long platformGetNativeDisplay() {
  38         return 0L;
  39     }
  40 
  41     protected long platformGetNativeWindow() {
  42         return 0L;
  43     }
  44 
  45     public AcceleratedScreen(int[] attributes) {
  46         initPlatformLibraries();
  47 
  48         int major[] = {0}, minor[]={0};
  49         eglDisplay =
  50                 EGL.eglGetDisplay(platformGetNativeDisplay());
  51         EGL.eglInitialize(eglDisplay, major, minor);
  52         EGL.eglBindAPI(EGL.EGL_OPENGL_ES_BIT);
  53 
  54         long eglConfigs[] = {0};
  55         int configCount[] = {0};
  56 
  57         EGL.eglChooseConfig(eglDisplay, attributes, eglConfigs, 1, configCount);
  58         eglSurface =
  59                 EGL.eglCreateWindowSurface(eglDisplay, eglConfigs[0],
  60                         platformGetNativeWindow(), null);
  61 
  62         int emptyAttrArray [] = {};
  63         eglContext = EGL.eglCreateContext(eglDisplay, eglConfigs[0],
  64                 0, emptyAttrArray);
  65     }
  66 
  67     public void enableRendering(boolean flag) {
  68         if (flag) {
  69             EGL.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
  70         } else {
  71             EGL.eglMakeCurrent(eglDisplay, 0, 0, eglContext);
  72         }
  73     }
  74 
  75     protected boolean initPlatformLibraries() {
  76         if (!initialized) {
  77             LinuxSystem ls = LinuxSystem.getLinuxSystem();
  78             glesLibraryHandle = ls.dlopen("libGLESv2.so",
  79                     LinuxSystem.RTLD_LAZY | LinuxSystem.RTLD_GLOBAL);
  80             eglLibraryHandle = ls.dlopen("libEGL.so",
  81                     LinuxSystem.RTLD_LAZY | LinuxSystem.RTLD_GLOBAL);
  82             initialized = true;
  83         }
  84         return true;
  85     }
  86 
  87     public long getGLHandle() {
  88         return glesLibraryHandle;
  89     }
  90 
  91     public long getEGLHandle() { return eglLibraryHandle; }
  92 
  93     public boolean swapBuffers() {
  94         EGL.eglSwapBuffers(eglDisplay, eglSurface);
  95         return true;
  96     }
  97 
  98 }