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.graphics.SurfaceTexture;
31 import android.view.Surface;
32 
33 import javax.microedition.khronos.egl.EGL10;
34 
35 
36 /**
37  * Holds EGL state and utility methods for handling an egl 1.0 EGLContext, an EGLDisplay,
38  * and an EGLSurface.
39  */
40 public abstract class EglBase {
41   // EGL wrapper for an actual EGLContext.
42   public static class Context {
43   }
44 
45   // These constants are taken from EGL14.EGL_OPENGL_ES2_BIT and EGL14.EGL_CONTEXT_CLIENT_VERSION.
46   // https://android.googlesource.com/platform/frameworks/base/+/master/opengl/java/android/opengl/EGL14.java
47   // This is similar to how GlSurfaceView does:
48   // http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/opengl/GLSurfaceView.java#760
49   private static final int EGL_OPENGL_ES2_BIT = 4;
50   // Android-specific extension.
51   private static final int EGL_RECORDABLE_ANDROID = 0x3142;
52 
53   public static final int[] CONFIG_PLAIN = {
54     EGL10.EGL_RED_SIZE, 8,
55     EGL10.EGL_GREEN_SIZE, 8,
56     EGL10.EGL_BLUE_SIZE, 8,
57     EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
58     EGL10.EGL_NONE
59   };
60   public static final int[] CONFIG_RGBA = {
61     EGL10.EGL_RED_SIZE, 8,
62     EGL10.EGL_GREEN_SIZE, 8,
63     EGL10.EGL_BLUE_SIZE, 8,
64     EGL10.EGL_ALPHA_SIZE, 8,
65     EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
66     EGL10.EGL_NONE
67   };
68   public static final int[] CONFIG_PIXEL_BUFFER = {
69     EGL10.EGL_RED_SIZE, 8,
70     EGL10.EGL_GREEN_SIZE, 8,
71     EGL10.EGL_BLUE_SIZE, 8,
72     EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
73     EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
74     EGL10.EGL_NONE
75   };
76   public static final int[] CONFIG_PIXEL_RGBA_BUFFER = {
77     EGL10.EGL_RED_SIZE, 8,
78     EGL10.EGL_GREEN_SIZE, 8,
79     EGL10.EGL_BLUE_SIZE, 8,
80     EGL10.EGL_ALPHA_SIZE, 8,
81     EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
82     EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
83     EGL10.EGL_NONE
84   };
85   public static final int[] CONFIG_RECORDABLE = {
86     EGL10.EGL_RED_SIZE, 8,
87     EGL10.EGL_GREEN_SIZE, 8,
88     EGL10.EGL_BLUE_SIZE, 8,
89     EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
90     EGL_RECORDABLE_ANDROID, 1,
91     EGL10.EGL_NONE
92   };
93 
94   // Create a new context with the specified config attributes, sharing data with sharedContext.
95   // |sharedContext| can be null.
create(Context sharedContext, int[] configAttributes)96   public static EglBase create(Context sharedContext, int[] configAttributes) {
97     return (EglBase14.isEGL14Supported()
98         && (sharedContext == null || sharedContext instanceof EglBase14.Context))
99             ? new EglBase14((EglBase14.Context) sharedContext, configAttributes)
100             : new EglBase10((EglBase10.Context) sharedContext, configAttributes);
101   }
102 
create()103   public static EglBase create() {
104     return create(null, CONFIG_PLAIN);
105   }
106 
createSurface(Surface surface)107   public abstract void createSurface(Surface surface);
108 
109   // Create EGLSurface from the Android SurfaceTexture.
createSurface(SurfaceTexture surfaceTexture)110   public abstract void createSurface(SurfaceTexture surfaceTexture);
111 
112   // Create dummy 1x1 pixel buffer surface so the context can be made current.
createDummyPbufferSurface()113   public abstract void createDummyPbufferSurface();
114 
createPbufferSurface(int width, int height)115   public abstract void createPbufferSurface(int width, int height);
116 
getEglBaseContext()117   public abstract Context getEglBaseContext();
118 
hasSurface()119   public abstract boolean hasSurface();
120 
surfaceWidth()121   public abstract int surfaceWidth();
122 
surfaceHeight()123   public abstract int surfaceHeight();
124 
releaseSurface()125   public abstract void releaseSurface();
126 
release()127   public abstract void release();
128 
makeCurrent()129   public abstract void makeCurrent();
130 
131   // Detach the current EGL context, so that it can be made current on another thread.
detachCurrent()132   public abstract void detachCurrent();
133 
swapBuffers()134   public abstract void swapBuffers();
135 }
136