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 /** AcceleratedScreen provides methods necessary to instantiate and intitialize 29 * a hardware-accelerated screen for rendering. 30 */ 31 public class AcceleratedScreen { 32 33 private static long glesLibraryHandle; 34 private static long eglLibraryHandle; 35 private static boolean initialized = false; 36 private long eglSurface; 37 private long eglContext; 38 private long eglDisplay; 39 protected static final LinuxSystem ls = LinuxSystem.getLinuxSystem(); 40 private EGL egl; 41 42 /** Returns a platform-specific native display handle suitable for use with 43 * eglGetDisplay. 44 */ 45 protected long platformGetNativeDisplay() { 46 return 0L; 47 } 48 49 /** Returns a platform-specific native window handle suitable for use with 50 * eglCreateWindowSurface. 51 */ 52 protected long platformGetNativeWindow() { 53 return 0L; 54 } 55 56 /** 57 * Perform basic egl intialization - open the display, create the drawing 58 * surface, and create a GL context to that drawing surface. 59 * @param attributes - attributes to be used for filtering the EGL 60 * configurations to choose from 61 * @throws GLException 62 * @throws UnsatisfiedLinkError 63 */ 64 AcceleratedScreen(int[] attributes) throws GLException, UnsatisfiedLinkError { 65 egl = EGL.getEGL(); 66 initPlatformLibraries(); 67 68 int major[] = {0}, minor[]={0}; 69 long nativeDisplay = platformGetNativeDisplay(); 70 long nativeWindow = platformGetNativeWindow(); 71 72 if (nativeDisplay == -1l) { // error condition 73 throw new GLException(0, "Could not get native display"); 74 } 75 if (nativeWindow == -1l) { // error condition 76 throw new GLException(0, "Could not get native window"); 77 } 78 79 eglDisplay = 80 egl.eglGetDisplay(nativeDisplay); 81 if (eglDisplay == EGL.EGL_NO_DISPLAY) { 82 throw new GLException(egl.eglGetError(), 83 "Could not get EGL display"); 84 } 85 86 if (!egl.eglInitialize(eglDisplay, major, minor)) { 87 throw new GLException(egl.eglGetError(), 88 "Error initializing EGL"); 89 } 90 91 if (!egl.eglBindAPI(EGL.EGL_OPENGL_ES_API)) { 92 throw new GLException(egl.eglGetError(), 93 "Error binding OPENGL API"); 94 } 95 96 long eglConfigs[] = {0}; 97 int configCount[] = {0}; 98 99 if (!egl.eglChooseConfig(eglDisplay, attributes, eglConfigs, 100 1, configCount)) { 101 throw new GLException(egl.eglGetError(), 102 "Error choosing EGL config"); 103 } 104 105 eglSurface = 106 egl.eglCreateWindowSurface(eglDisplay, eglConfigs[0], 107 nativeWindow, null); 108 if (eglSurface == EGL.EGL_NO_SURFACE) { 109 throw new GLException(egl.eglGetError(), 110 "Could not get EGL surface"); 111 } 112 113 int emptyAttrArray [] = {}; 114 eglContext = egl.eglCreateContext(eglDisplay, eglConfigs[0], 115 0, emptyAttrArray); 116 if (eglContext == EGL.EGL_NO_CONTEXT) { 117 throw new GLException(egl.eglGetError(), 118 "Could not get EGL context"); 119 } 120 } 121 122 /** Make the EGL drawing surface current or not 123 * 124 * @param flag 125 */ 126 public void enableRendering(boolean flag) { 127 if (flag) { 128 egl.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, 129 eglContext); 130 } else { 131 egl.eglMakeCurrent(eglDisplay, 0, 0, eglContext); 132 } 133 } 134 135 /** Load any native libraries needed to instantiate and initialize the 136 * native drawing surface and rendering context 137 * @return success or failure 138 * @throws UnsatisfiedLinkError 139 */ 140 boolean initPlatformLibraries() throws UnsatisfiedLinkError{ 141 if (!initialized) { 142 glesLibraryHandle = ls.dlopen("libGLESv2.so", 143 LinuxSystem.RTLD_LAZY | LinuxSystem.RTLD_GLOBAL); 144 if (glesLibraryHandle == 0l) { 145 throw new UnsatisfiedLinkError("Error loading libGLESv2.so"); 146 } 147 eglLibraryHandle = ls.dlopen("libEGL.so", 148 LinuxSystem.RTLD_LAZY | LinuxSystem.RTLD_GLOBAL); 149 if (eglLibraryHandle == 0l) { 150 throw new UnsatisfiedLinkError("Error loading libEGL.so"); 151 } 152 initialized = true; 153 } 154 return true; 155 } 156 157 /** Return the GL library handle - for use in looking up native symbols 158 * 159 */ 160 public long getGLHandle() { 161 return glesLibraryHandle; 162 } 163 164 /** Return the EGL library handle - for use in looking up native symbols 165 * 166 */ 167 protected long getEGLHandle() { return eglLibraryHandle; } 168 169 /** Copy the contents of the GL backbuffer to the screen 170 * 171 * @return success or failure 172 */ 173 public boolean swapBuffers() { 174 synchronized(NativeScreen.framebufferSwapLock) { 175 egl.eglSwapBuffers(eglDisplay, eglSurface); 176 } 177 return true; 178 } 179 180 }