blob: e94037d9bf048420e98debbec2c8b642b7d3ad03 [file] [log] [blame]
Magnus Jedvert80cf97c2015-06-11 10:08:59 +02001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
Magnus Jedvert80cf97c2015-06-11 10:08:59 +02003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
Magnus Jedvert80cf97c2015-06-11 10:08:59 +02009 */
10
11package org.webrtc;
12
Magnus Jedvert1ab271c2015-09-28 11:05:44 +020013import android.graphics.SurfaceTexture;
magjed3e0f6022015-11-16 02:04:50 -080014import android.view.Surface;
magjed8c425aa2015-10-22 16:52:39 -070015import javax.microedition.khronos.egl.EGL10;
perkj96381432015-12-15 02:48:07 -080016
Magnus Jedvert80cf97c2015-06-11 10:08:59 +020017/**
perkj40455d62015-12-02 01:07:18 -080018 * Holds EGL state and utility methods for handling an egl 1.0 EGLContext, an EGLDisplay,
19 * and an EGLSurface.
Magnus Jedvert80cf97c2015-06-11 10:08:59 +020020 */
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +010021public interface EglBase {
perkj96381432015-12-15 02:48:07 -080022 // EGL wrapper for an actual EGLContext.
Magnus Jedvertaa568a62017-09-18 11:26:30 +020023 public interface Context { long getNativeEglContext(); }
perkj96381432015-12-15 02:48:07 -080024
Magnus Jedvert3db6f9b2016-03-31 13:17:11 +020025 // According to the documentation, EGL can be used from multiple threads at the same time if each
26 // thread has its own EGLContext, but in practice it deadlocks on some devices when doing this.
27 // Therefore, synchronize on this global lock before calling dangerous EGL functions that might
28 // deadlock. See https://bugs.chromium.org/p/webrtc/issues/detail?id=5702 for more info.
29 public static final Object lock = new Object();
30
magjed8c425aa2015-10-22 16:52:39 -070031 // These constants are taken from EGL14.EGL_OPENGL_ES2_BIT and EGL14.EGL_CONTEXT_CLIENT_VERSION.
32 // https://android.googlesource.com/platform/frameworks/base/+/master/opengl/java/android/opengl/EGL14.java
33 // This is similar to how GlSurfaceView does:
34 // http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/opengl/GLSurfaceView.java#760
magjed47815522017-08-25 06:28:00 -070035 public static final int EGL_OPENGL_ES2_BIT = 4;
Magnus Jedvert80cf97c2015-06-11 10:08:59 +020036 // Android-specific extension.
magjed47815522017-08-25 06:28:00 -070037 public static final int EGL_RECORDABLE_ANDROID = 0x3142;
Magnus Jedvert80cf97c2015-06-11 10:08:59 +020038
sakalb6760f92016-09-29 04:12:44 -070039 // clang-format off
nisse03f80eb2015-12-07 01:17:16 -080040 public static final int[] CONFIG_PLAIN = {
41 EGL10.EGL_RED_SIZE, 8,
42 EGL10.EGL_GREEN_SIZE, 8,
43 EGL10.EGL_BLUE_SIZE, 8,
44 EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
45 EGL10.EGL_NONE
46 };
Magnus Jedvert51254332015-12-15 16:22:29 +010047 public static final int[] CONFIG_RGBA = {
48 EGL10.EGL_RED_SIZE, 8,
49 EGL10.EGL_GREEN_SIZE, 8,
50 EGL10.EGL_BLUE_SIZE, 8,
51 EGL10.EGL_ALPHA_SIZE, 8,
52 EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
53 EGL10.EGL_NONE
54 };
nisse03f80eb2015-12-07 01:17:16 -080055 public static final int[] CONFIG_PIXEL_BUFFER = {
56 EGL10.EGL_RED_SIZE, 8,
57 EGL10.EGL_GREEN_SIZE, 8,
58 EGL10.EGL_BLUE_SIZE, 8,
59 EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
60 EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
61 EGL10.EGL_NONE
62 };
nissec490e012015-12-10 06:23:33 -080063 public static final int[] CONFIG_PIXEL_RGBA_BUFFER = {
64 EGL10.EGL_RED_SIZE, 8,
65 EGL10.EGL_GREEN_SIZE, 8,
66 EGL10.EGL_BLUE_SIZE, 8,
67 EGL10.EGL_ALPHA_SIZE, 8,
68 EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
69 EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
70 EGL10.EGL_NONE
71 };
nisse03f80eb2015-12-07 01:17:16 -080072 public static final int[] CONFIG_RECORDABLE = {
73 EGL10.EGL_RED_SIZE, 8,
74 EGL10.EGL_GREEN_SIZE, 8,
75 EGL10.EGL_BLUE_SIZE, 8,
76 EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
77 EGL_RECORDABLE_ANDROID, 1,
78 EGL10.EGL_NONE
79 };
sakalb6760f92016-09-29 04:12:44 -070080 // clang-format on
Magnus Jedvert80cf97c2015-06-11 10:08:59 +020081
magjedb04646f2017-04-21 01:34:12 -070082 /**
83 * Create a new context with the specified config attributes, sharing data with |sharedContext|.
84 * If |sharedContext| is null, a root context is created. This function will try to create an EGL
85 * 1.4 context if possible, and an EGL 1.0 context otherwise.
86 */
nisse03f80eb2015-12-07 01:17:16 -080087 public static EglBase create(Context sharedContext, int[] configAttributes) {
perkj40455d62015-12-02 01:07:18 -080088 return (EglBase14.isEGL14Supported()
sakalb6760f92016-09-29 04:12:44 -070089 && (sharedContext == null || sharedContext instanceof EglBase14.Context))
90 ? new EglBase14((EglBase14.Context) sharedContext, configAttributes)
91 : new EglBase10((EglBase10.Context) sharedContext, configAttributes);
perkj40455d62015-12-02 01:07:18 -080092 }
93
magjedb04646f2017-04-21 01:34:12 -070094 /**
95 * Helper function for creating a plain root context. This function will try to create an EGL 1.4
96 * context if possible, and an EGL 1.0 context otherwise.
97 */
perkj40455d62015-12-02 01:07:18 -080098 public static EglBase create() {
magjedb04646f2017-04-21 01:34:12 -070099 return create(null /* shaderContext */, CONFIG_PLAIN);
perkj40455d62015-12-02 01:07:18 -0800100 }
101
magjedb04646f2017-04-21 01:34:12 -0700102 /**
103 * Helper function for creating a plain context, sharing data with |sharedContext|. This function
104 * will try to create an EGL 1.4 context if possible, and an EGL 1.0 context otherwise.
105 */
Perec2922f2016-01-27 15:25:46 +0100106 public static EglBase create(Context sharedContext) {
107 return create(sharedContext, CONFIG_PLAIN);
108 }
109
magjedb04646f2017-04-21 01:34:12 -0700110 /**
111 * Explicitly create a root EGl 1.0 context with the specified config attributes.
112 */
113 public static EglBase createEgl10(int[] configAttributes) {
114 return new EglBase10(null /* shaderContext */, configAttributes);
115 }
116
117 /**
deadbeefa615e172017-05-25 10:11:25 -0700118 * Explicitly create a root EGl 1.0 context with the specified config attributes
119 * and shared context.
120 */
121 public static EglBase createEgl10(
122 javax.microedition.khronos.egl.EGLContext sharedContext, int[] configAttributes) {
123 return new EglBase10(new EglBase10.Context(sharedContext), configAttributes);
124 }
125
126 /**
magjedb04646f2017-04-21 01:34:12 -0700127 * Explicitly create a root EGl 1.4 context with the specified config attributes.
128 */
129 public static EglBase createEgl14(int[] configAttributes) {
130 return new EglBase14(null /* shaderContext */, configAttributes);
131 }
132
deadbeefa615e172017-05-25 10:11:25 -0700133 /**
134 * Explicitly create a root EGl 1.4 context with the specified config attributes
135 * and shared context.
136 */
137 public static EglBase createEgl14(
138 android.opengl.EGLContext sharedContext, int[] configAttributes) {
139 return new EglBase14(new EglBase14.Context(sharedContext), configAttributes);
140 }
141
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100142 void createSurface(Surface surface);
Magnus Jedvert1ab271c2015-09-28 11:05:44 +0200143
144 // Create EGLSurface from the Android SurfaceTexture.
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100145 void createSurface(SurfaceTexture surfaceTexture);
Magnus Jedvert80cf97c2015-06-11 10:08:59 +0200146
147 // Create dummy 1x1 pixel buffer surface so the context can be made current.
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100148 void createDummyPbufferSurface();
magjedb28678c2015-07-26 05:17:19 -0700149
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100150 void createPbufferSurface(int width, int height);
Magnus Jedvert80cf97c2015-06-11 10:08:59 +0200151
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100152 Context getEglBaseContext();
Magnus Jedvert80cf97c2015-06-11 10:08:59 +0200153
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100154 boolean hasSurface();
Magnus Jedvert80cf97c2015-06-11 10:08:59 +0200155
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100156 int surfaceWidth();
Magnus Jedvertd476b952015-08-20 09:58:31 +0200157
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100158 int surfaceHeight();
Magnus Jedvertd476b952015-08-20 09:58:31 +0200159
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100160 void releaseSurface();
Magnus Jedvert80cf97c2015-06-11 10:08:59 +0200161
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100162 void release();
Magnus Jedvert80cf97c2015-06-11 10:08:59 +0200163
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100164 void makeCurrent();
Magnus Jedvert80cf97c2015-06-11 10:08:59 +0200165
Magnus Jedvert4ae28a12015-09-15 09:44:07 +0200166 // Detach the current EGL context, so that it can be made current on another thread.
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100167 void detachCurrent();
Magnus Jedvert4ae28a12015-09-15 09:44:07 +0200168
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100169 void swapBuffers();
magjed47815522017-08-25 06:28:00 -0700170
Sami Kalliomäkib9f3f9b2017-11-30 11:23:33 +0100171 void swapBuffers(long presentationTimeStampNs);
Magnus Jedvert80cf97c2015-06-11 10:08:59 +0200172}