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     protected static LinuxSystem ls = LinuxSystem.getLinuxSystem();
  37 
  38     protected long platformGetNativeDisplay() {
  39         return 0L;
  40     }
  41 
  42     protected long platformGetNativeWindow() {
  43         return 0L;
  44     }
  45 
  46     public AcceleratedScreen(int[] attributes) {
  47         initPlatformLibraries();
  48 
  49         int major[] = {0}, minor[]={0};
  50         eglDisplay =
  51                 EGL.eglGetDisplay(platformGetNativeDisplay());
  52         EGL.eglInitialize(eglDisplay, major, minor);
  53         EGL.eglBindAPI(EGL.EGL_OPENGL_ES_BIT);
  54 
  55         long eglConfigs[] = {0};
  56         int configCount[] = {0};
  57 
  58         EGL.eglChooseConfig(eglDisplay, attributes, eglConfigs, 1, configCount);
  59         eglSurface =
  60                 EGL.eglCreateWindowSurface(eglDisplay, eglConfigs[0],
  61                         platformGetNativeWindow(), null);
  62 
  63         int emptyAttrArray [] = {};
  64         eglContext = EGL.eglCreateContext(eglDisplay, eglConfigs[0],
  65                 0, emptyAttrArray);
  66     }
  67 
  68     public void enableRendering(boolean flag) {
  69         if (flag) {
  70             EGL.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
  71         } else {
  72             EGL.eglMakeCurrent(eglDisplay, 0, 0, eglContext);
  73         }
  74     }
  75 
  76     protected boolean initPlatformLibraries() {
  77         if (!initialized) {
  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 }