OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * libjingle |
| 3 * Copyright 2015 Google Inc. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: |
| 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 * this list of conditions and the following disclaimer in the documentation |
| 12 * and/or other materials provided with the distribution. |
| 13 * 3. The name of the author may not be used to endorse or promote products |
| 14 * derived from this software without specific prior written permission. |
| 15 * |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ |
| 27 |
| 28 package org.webrtc; |
| 29 |
| 30 import android.annotation.TargetApi; |
| 31 import android.graphics.SurfaceTexture; |
| 32 import android.opengl.EGL14; |
| 33 import android.opengl.EGLConfig; |
| 34 import android.opengl.EGLContext; |
| 35 import android.opengl.EGLDisplay; |
| 36 import android.opengl.EGLSurface; |
| 37 import android.view.Surface; |
| 38 |
| 39 import org.webrtc.Logging; |
| 40 |
| 41 /** |
| 42 * Holds EGL state and utility methods for handling an EGL14 EGLContext, an EGLD
isplay, |
| 43 * and an EGLSurface. |
| 44 */ |
| 45 @TargetApi(17) |
| 46 final class EglBase14 extends EglBase { |
| 47 private static final String TAG = "EglBase14"; |
| 48 private static final int EGL14_SDK_VERSION = android.os.Build.VERSION_CODES.JE
LLY_BEAN_MR1; |
| 49 private static final int CURRENT_SDK_VERSION = android.os.Build.VERSION.SDK_IN
T; |
| 50 // Android-specific extension. |
| 51 private static final int EGL_RECORDABLE_ANDROID = 0x3142; |
| 52 |
| 53 private EGLContext eglContext; |
| 54 private ConfigType configType; |
| 55 private EGLConfig eglConfig; |
| 56 private EGLDisplay eglDisplay; |
| 57 private EGLSurface eglSurface = EGL14.EGL_NO_SURFACE; |
| 58 |
| 59 public static boolean isEGL14Supported() { |
| 60 Logging.d(TAG, "SDK version: " + CURRENT_SDK_VERSION |
| 61 + ". isEGL14Supported: " + (CURRENT_SDK_VERSION >= EGL14_SDK_VERSION)); |
| 62 return (CURRENT_SDK_VERSION >= EGL14_SDK_VERSION); |
| 63 } |
| 64 |
| 65 public static class Context extends EglBase.Context { |
| 66 private final android.opengl.EGLContext egl14Context; |
| 67 |
| 68 Context(android.opengl.EGLContext eglContext) { |
| 69 super(null); |
| 70 this.egl14Context = eglContext; |
| 71 } |
| 72 } |
| 73 |
| 74 // Create a new context with the specified config type, sharing data with shar
edContext. |
| 75 // |sharedContext| may be null. |
| 76 EglBase14(EglBase14.Context sharedContext, ConfigType configType) { |
| 77 super(true /* dummy */); |
| 78 this.configType = configType; |
| 79 eglDisplay = getEglDisplay(); |
| 80 eglConfig = getEglConfig(eglDisplay, configType); |
| 81 eglContext = createEglContext(sharedContext, eglDisplay, eglConfig); |
| 82 } |
| 83 |
| 84 // Create EGLSurface from the Android Surface. |
| 85 @Override |
| 86 public void createSurface(Surface surface) { |
| 87 createSurfaceInternal(surface); |
| 88 } |
| 89 |
| 90 // Create EGLSurface from the Android SurfaceTexture. |
| 91 @Override |
| 92 public void createSurface(SurfaceTexture surfaceTexture) { |
| 93 createSurfaceInternal(surfaceTexture); |
| 94 } |
| 95 |
| 96 // Create EGLSurface from either Surface or SurfaceTexture. |
| 97 private void createSurfaceInternal(Object surface) { |
| 98 if (!(surface instanceof Surface) && !(surface instanceof SurfaceTexture)) { |
| 99 throw new IllegalStateException("Input must be either a Surface or Surface
Texture"); |
| 100 } |
| 101 checkIsNotReleased(); |
| 102 if (configType == ConfigType.PIXEL_BUFFER) { |
| 103 Logging.w(TAG, "This EGL context is configured for PIXEL_BUFFER, but uses
regular Surface"); |
| 104 } |
| 105 if (eglSurface != EGL14.EGL_NO_SURFACE) { |
| 106 throw new RuntimeException("Already has an EGLSurface"); |
| 107 } |
| 108 int[] surfaceAttribs = {EGL14.EGL_NONE}; |
| 109 eglSurface = EGL14.eglCreateWindowSurface(eglDisplay, eglConfig, surface, su
rfaceAttribs, 0); |
| 110 if (eglSurface == EGL14.EGL_NO_SURFACE) { |
| 111 throw new RuntimeException("Failed to create window surface"); |
| 112 } |
| 113 } |
| 114 |
| 115 @Override |
| 116 public void createDummyPbufferSurface() { |
| 117 createPbufferSurface(1, 1); |
| 118 } |
| 119 |
| 120 @Override |
| 121 public void createPbufferSurface(int width, int height) { |
| 122 checkIsNotReleased(); |
| 123 if (configType != ConfigType.PIXEL_BUFFER) { |
| 124 throw new RuntimeException( |
| 125 "This EGL context is not configured to use a pixel buffer: " + configT
ype); |
| 126 } |
| 127 if (eglSurface != EGL14.EGL_NO_SURFACE) { |
| 128 throw new RuntimeException("Already has an EGLSurface"); |
| 129 } |
| 130 int[] surfaceAttribs = {EGL14.EGL_WIDTH, width, EGL14.EGL_HEIGHT, height, EG
L14.EGL_NONE}; |
| 131 eglSurface = EGL14.eglCreatePbufferSurface(eglDisplay, eglConfig, surfaceAtt
ribs, 0); |
| 132 if (eglSurface == EGL14.EGL_NO_SURFACE) { |
| 133 throw new RuntimeException("Failed to create pixel buffer surface"); |
| 134 } |
| 135 } |
| 136 |
| 137 @Override |
| 138 public Context getEglBaseContext() { |
| 139 return new EglBase14.Context(eglContext); |
| 140 } |
| 141 |
| 142 @Override |
| 143 public boolean hasSurface() { |
| 144 return eglSurface != EGL14.EGL_NO_SURFACE; |
| 145 } |
| 146 |
| 147 @Override |
| 148 public int surfaceWidth() { |
| 149 final int widthArray[] = new int[1]; |
| 150 EGL14.eglQuerySurface(eglDisplay, eglSurface, EGL14.EGL_WIDTH, widthArray, 0
); |
| 151 return widthArray[0]; |
| 152 } |
| 153 |
| 154 @Override |
| 155 public int surfaceHeight() { |
| 156 final int heightArray[] = new int[1]; |
| 157 EGL14.eglQuerySurface(eglDisplay, eglSurface, EGL14.EGL_HEIGHT, heightArray,
0); |
| 158 return heightArray[0]; |
| 159 } |
| 160 |
| 161 @Override |
| 162 public void releaseSurface() { |
| 163 if (eglSurface != EGL14.EGL_NO_SURFACE) { |
| 164 EGL14.eglDestroySurface(eglDisplay, eglSurface); |
| 165 eglSurface = EGL14.EGL_NO_SURFACE; |
| 166 } |
| 167 } |
| 168 |
| 169 private void checkIsNotReleased() { |
| 170 if (eglDisplay == EGL14.EGL_NO_DISPLAY || eglContext == EGL14.EGL_NO_CONTEXT |
| 171 || eglConfig == null) { |
| 172 throw new RuntimeException("This object has been released"); |
| 173 } |
| 174 } |
| 175 |
| 176 @Override |
| 177 public void release() { |
| 178 checkIsNotReleased(); |
| 179 releaseSurface(); |
| 180 detachCurrent(); |
| 181 EGL14.eglDestroyContext(eglDisplay, eglContext); |
| 182 EGL14.eglReleaseThread(); |
| 183 EGL14.eglTerminate(eglDisplay); |
| 184 eglContext = EGL14.EGL_NO_CONTEXT; |
| 185 eglDisplay = EGL14.EGL_NO_DISPLAY; |
| 186 eglConfig = null; |
| 187 } |
| 188 |
| 189 @Override |
| 190 public void makeCurrent() { |
| 191 checkIsNotReleased(); |
| 192 if (eglSurface == EGL14.EGL_NO_SURFACE) { |
| 193 throw new RuntimeException("No EGLSurface - can't make current"); |
| 194 } |
| 195 if (!EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) { |
| 196 throw new RuntimeException("eglMakeCurrent failed"); |
| 197 } |
| 198 } |
| 199 |
| 200 // Detach the current EGL context, so that it can be made current on another t
hread. |
| 201 @Override |
| 202 public void detachCurrent() { |
| 203 if (!EGL14.eglMakeCurrent( |
| 204 eglDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CON
TEXT)) { |
| 205 throw new RuntimeException("eglMakeCurrent failed"); |
| 206 } |
| 207 } |
| 208 |
| 209 @Override |
| 210 public void swapBuffers() { |
| 211 checkIsNotReleased(); |
| 212 if (eglSurface == EGL14.EGL_NO_SURFACE) { |
| 213 throw new RuntimeException("No EGLSurface - can't swap buffers"); |
| 214 } |
| 215 EGL14.eglSwapBuffers(eglDisplay, eglSurface); |
| 216 } |
| 217 |
| 218 // Return an EGLDisplay, or die trying. |
| 219 private static EGLDisplay getEglDisplay() { |
| 220 EGLDisplay eglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); |
| 221 if (eglDisplay == EGL14.EGL_NO_DISPLAY) { |
| 222 throw new RuntimeException("Unable to get EGL14 display"); |
| 223 } |
| 224 int[] version = new int[2]; |
| 225 if (!EGL14.eglInitialize(eglDisplay, version, 0, version, 1)) { |
| 226 throw new RuntimeException("Unable to initialize EGL14"); |
| 227 } |
| 228 return eglDisplay; |
| 229 } |
| 230 |
| 231 // Return an EGLConfig, or die trying. |
| 232 private static EGLConfig getEglConfig(EGLDisplay eglDisplay, ConfigType config
Type) { |
| 233 // Always RGB888, GLES2. |
| 234 int[] configAttributes = { |
| 235 EGL14.EGL_RED_SIZE, 8, |
| 236 EGL14.EGL_GREEN_SIZE, 8, |
| 237 EGL14.EGL_BLUE_SIZE, 8, |
| 238 EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT, |
| 239 EGL14.EGL_NONE, 0, // Allocate dummy fields for specific options. |
| 240 EGL14.EGL_NONE |
| 241 }; |
| 242 |
| 243 // Fill in dummy fields based on configType. |
| 244 switch (configType) { |
| 245 case PLAIN: |
| 246 break; |
| 247 case PIXEL_BUFFER: |
| 248 configAttributes[configAttributes.length - 3] = EGL14.EGL_SURFACE_TYPE; |
| 249 configAttributes[configAttributes.length - 2] = EGL14.EGL_PBUFFER_BIT; |
| 250 break; |
| 251 case RECORDABLE: |
| 252 configAttributes[configAttributes.length - 3] = EGL_RECORDABLE_ANDROID; |
| 253 configAttributes[configAttributes.length - 2] = 1; |
| 254 break; |
| 255 default: |
| 256 throw new IllegalArgumentException(); |
| 257 } |
| 258 |
| 259 EGLConfig[] configs = new EGLConfig[1]; |
| 260 int[] numConfigs = new int[1]; |
| 261 if (!EGL14.eglChooseConfig( |
| 262 eglDisplay, configAttributes, 0, configs, 0, configs.length, numConfigs,
0)) { |
| 263 throw new RuntimeException("Unable to find RGB888 " + configType + " EGL c
onfig"); |
| 264 } |
| 265 return configs[0]; |
| 266 } |
| 267 |
| 268 // Return an EGLConfig, or die trying. |
| 269 private static EGLContext createEglContext( |
| 270 EglBase14.Context sharedContext, EGLDisplay eglDisplay, EGLConfig eglConfi
g) { |
| 271 int[] contextAttributes = {EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NO
NE}; |
| 272 EGLContext rootContext = |
| 273 sharedContext == null ? EGL14.EGL_NO_CONTEXT : sharedContext.egl14Contex
t; |
| 274 EGLContext eglContext = |
| 275 EGL14.eglCreateContext(eglDisplay, eglConfig, rootContext, contextAttrib
utes, 0); |
| 276 if (eglContext == EGL14.EGL_NO_CONTEXT) { |
| 277 throw new RuntimeException("Failed to create EGL context"); |
| 278 } |
| 279 return eglContext; |
| 280 } |
| 281 } |
OLD | NEW |