-
Notifications
You must be signed in to change notification settings - Fork 0
/
EglCore.cs
379 lines (346 loc) · 13.2 KB
/
EglCore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using Android.Graphics;
using Android.Opengl;
using Android.Util;
using Android.Views;
namespace Grafika.GLES
{
public class EglCore : Java.Lang.Object
{
private const string TAG = GlUtil.TAG;
/**
* Constructor flag: surface must be recordable. This discourages EGL from using a
* pixel format that cannot be converted efficiently to something usable by the video
* encoder.
*/
public const int FLAG_RECORDABLE = 0x01;
/**
* Constructor flag: ask for GLES3, fall back to GLES2 if not available. Without this
* flag, GLES2 is used.
*/
public const int FLAG_TRY_GLES3 = 0x02;
// Android-specific extension.
private const int EGL_RECORDABLE_ANDROID = 0x3142;
private EGLDisplay mEGLDisplay = EGL14.EglNoDisplay;
private EGLContext mEGLContext = EGL14.EglNoContext;
private EGLConfig mEGLConfig = null;
private int mGlVersion = -1;
/**
* Prepares EGL display and context.
* <p>
* @param sharedContext The context to share, or null if sharing is not desired.
* @param flags Configuration bit flags, e.g. FLAG_RECORDABLE.
*/
public EglCore(EGLContext sharedContext = null, int flags = 0)
{
if (mEGLDisplay != EGL14.EglNoDisplay)
{
throw new Exception("EGL already set up");
}
if (sharedContext == null)
{
sharedContext = EGL14.EglNoContext;
}
mEGLDisplay = EGL14.EglGetDisplay(EGL14.EglDefaultDisplay);
if (mEGLDisplay == EGL14.EglNoDisplay)
{
throw new Exception("unable to get EGL14 display");
}
int[] version = new int[2];
if (!EGL14.EglInitialize(mEGLDisplay, version, 0, version, 1))
{
mEGLDisplay = null;
throw new Exception("unable to initialize EGL14");
}
// Try to get a GLES3 context, if requested.
if ((flags & FLAG_TRY_GLES3) != 0)
{
//Log.d(TAG, "Trying GLES 3");
EGLConfig config = getConfig(flags, 3);
if (config != null)
{
int[] attrib3_list = {
EGL14.EglContextClientVersion, 3,
EGL14.EglNone
};
EGLContext context = EGL14.EglCreateContext(mEGLDisplay, config, sharedContext,
attrib3_list, 0);
if (EGL14.EglGetError() == EGL14.EglSuccess)
{
//Log.d(TAG, "Got GLES 3 config");
mEGLConfig = config;
mEGLContext = context;
mGlVersion = 3;
}
}
}
if (mEGLContext == EGL14.EglNoContext)
{ // GLES 2 only, or GLES 3 attempt failed
//Log.d(TAG, "Trying GLES 2");
EGLConfig config = getConfig(flags, 2);
if (config == null)
{
throw new Exception("Unable to find a suitable EGLConfig");
}
int[] attrib2_list = {
EGL14.EglContextClientVersion, 2,
EGL14.EglNone
};
EGLContext context = EGL14.EglCreateContext(mEGLDisplay, config, sharedContext,
attrib2_list, 0);
checkEglError("eglCreateContext");
mEGLConfig = config;
mEGLContext = context;
mGlVersion = 2;
}
// Confirm with query.
int[] values = new int[1];
EGL14.EglQueryContext(mEGLDisplay, mEGLContext, EGL14.EglContextClientVersion,
values, 0);
Log.Debug(TAG, "EGLContext created, client version " + values[0]);
}
/**
* Finds a suitable EGLConfig.
*
* @param flags Bit flags from constructor.
* @param version Must be 2 or 3.
*/
private EGLConfig getConfig(int flags, int version)
{
int renderableType = EGL14.EglOpenglEs2Bit;
if (version >= 3)
{
renderableType |= EGLExt.EglOpenglEs3BitKhr;
}
// The actual surface is generally RGBA or RGBX, so situationally omitting alpha
// doesn't really help. It can also lead to a huge performance hit on glReadPixels()
// when reading into a GL_RGBA buffer.
int[] attribList = {
EGL14.EglRedSize, 8,
EGL14.EglGreenSize, 8,
EGL14.EglBlueSize, 8,
EGL14.EglAlphaSize, 8,
//EGL14.EGL_DEPTH_SIZE, 16,
//EGL14.EGL_STENCIL_SIZE, 8,
EGL14.EglRenderableType, renderableType,
EGL14.EglNone, 0, // placeholder for recordable [@-3]
EGL14.EglNone
};
if ((flags & FLAG_RECORDABLE) != 0)
{
attribList[attribList.Length - 3] = EGL_RECORDABLE_ANDROID;
attribList[attribList.Length - 2] = 1;
}
EGLConfig[] configs = new EGLConfig[1];
int[] numConfigs = new int[1];
if (!EGL14.EglChooseConfig(mEGLDisplay, attribList, 0, configs, 0, configs.Length,
numConfigs, 0))
{
Log.Warn(TAG, "unable to find RGB8888 / " + version + " EGLConfig");
return null;
}
return configs[0];
}
/**
* Discards all resources held by this class, notably the EGL context. This must be
* called from the thread where the context was created.
* <p>
* On completion, no context will be current.
*/
public void release()
{
if (mEGLDisplay != EGL14.EglNoDisplay)
{
// Android is unusual in that it uses a reference-counted EGLDisplay. So for
// every eglInitialize() we need an eglTerminate().
EGL14.EglMakeCurrent(mEGLDisplay, EGL14.EglNoSurface, EGL14.EglNoSurface,
EGL14.EglNoContext);
EGL14.EglDestroyContext(mEGLDisplay, mEGLContext);
EGL14.EglReleaseThread();
EGL14.EglTerminate(mEGLDisplay);
}
mEGLDisplay = EGL14.EglNoDisplay;
mEGLContext = EGL14.EglNoContext;
mEGLConfig = null;
}
protected override void JavaFinalize()
{
try {
if (mEGLDisplay != EGL14.EglNoDisplay)
{
// We're limited here -- finalizers don't run on the thread that holds
// the EGL state, so if a surface or context is still current on another
// thread we can't fully release it here. Exceptions thrown from here
// are quietly discarded. Complain in the log file.
Log.Warn(TAG, "WARNING: EglCore was not explicitly released -- state may be leaked");
release();
}
} finally {
base.JavaFinalize();
}
}
/**
* Destroys the specified surface. Note the EGLSurface won't actually be destroyed if it's
* still current in a context.
*/
public void releaseSurface(EGLSurface eglSurface)
{
EGL14.EglDestroySurface(mEGLDisplay, eglSurface);
}
/**
* Creates an EGL surface associated with a Surface.
* <p>
* If this is destined for MediaCodec, the EGLConfig should have the "recordable" attribute.
*/
public EGLSurface createWindowSurface(Java.Lang.Object surface)
{
if (!(surface is Surface) && !(surface is SurfaceTexture)) {
throw new Exception("invalid surface: " + surface);
}
// Create a window surface, and attach it to the Surface we received.
int[] surfaceAttribs = {
EGL14.EglNone
};
EGLSurface eglSurface = EGL14.EglCreateWindowSurface(mEGLDisplay, mEGLConfig, surface,
surfaceAttribs, 0);
checkEglError("eglCreateWindowSurface");
if (eglSurface == null)
{
throw new Exception("surface was null");
}
return eglSurface;
}
/**
* Creates an EGL surface associated with an offscreen buffer.
*/
public EGLSurface createOffscreenSurface(int width, int height)
{
int[] surfaceAttribs = {
EGL14.EglWidth, width,
EGL14.EglHeight, height,
EGL14.EglNone
};
EGLSurface eglSurface = EGL14.EglCreatePbufferSurface(mEGLDisplay, mEGLConfig,
surfaceAttribs, 0);
checkEglError("eglCreatePbufferSurface");
if (eglSurface == null)
{
throw new Exception("surface was null");
}
return eglSurface;
}
/**
* Makes our EGL context current, using the supplied surface for both "draw" and "read".
*/
public void makeCurrent(EGLSurface eglSurface)
{
if (mEGLDisplay == EGL14.EglNoDisplay)
{
// called makeCurrent() before create?
Log.Debug(TAG, "NOTE: makeCurrent w/o display");
}
if (!EGL14.EglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext))
{
throw new Exception("eglMakeCurrent failed");
}
}
/**
* Makes our EGL context current, using the supplied "draw" and "read" surfaces.
*/
public void makeCurrent(EGLSurface drawSurface, EGLSurface readSurface)
{
if (mEGLDisplay == EGL14.EglNoDisplay)
{
// called makeCurrent() before create?
Log.Debug(TAG, "NOTE: makeCurrent w/o display");
}
if (!EGL14.EglMakeCurrent(mEGLDisplay, drawSurface, readSurface, mEGLContext))
{
throw new Exception("eglMakeCurrent(draw,read) failed");
}
}
/**
* Makes no context current.
*/
public void makeNothingCurrent()
{
if (!EGL14.EglMakeCurrent(mEGLDisplay, EGL14.EglNoSurface, EGL14.EglNoSurface,
EGL14.EglNoContext))
{
throw new Exception("eglMakeCurrent failed");
}
}
/**
* Calls eglSwapBuffers. Use this to "publish" the current frame.
*
* @return false on failure
*/
public bool swapBuffers(EGLSurface eglSurface)
{
return EGL14.EglSwapBuffers(mEGLDisplay, eglSurface);
}
/**
* Sends the presentation time stamp to EGL. Time is expressed in nanoseconds.
*/
public void setPresentationTime(EGLSurface eglSurface, long nsecs)
{
EGLExt.EglPresentationTimeANDROID(mEGLDisplay, eglSurface, nsecs);
}
/**
* Returns true if our context and the specified surface are current.
*/
public bool isCurrent(EGLSurface eglSurface)
{
return mEGLContext.Equals(EGL14.EglGetCurrentContext()) &&
eglSurface.Equals(EGL14.EglGetCurrentSurface(EGL14.EglDraw));
}
/**
* Performs a simple surface query.
*/
public int querySurface(EGLSurface eglSurface, int what)
{
int[] value = new int[1];
EGL14.EglQuerySurface(mEGLDisplay, eglSurface, what, value, 0);
return value[0];
}
/**
* Queries a string value.
*/
public string queryString(int what)
{
return EGL14.EglQueryString(mEGLDisplay, what);
}
/**
* Returns the GLES version this context is configured for (currently 2 or 3).
*/
public int getGlVersion()
{
return mGlVersion;
}
/**
* Writes the current display, context, and surface to the log.
*/
public static void logCurrent(string msg)
{
EGLDisplay display;
EGLContext context;
EGLSurface surface;
display = EGL14.EglGetCurrentDisplay();
context = EGL14.EglGetCurrentContext();
surface = EGL14.EglGetCurrentSurface(EGL14.EglDraw);
Log.Info(TAG, "Current EGL (" + msg + "): display=" + display + ", context=" + context +
", surface=" + surface);
}
/**
* Checks for EGL errors. Throws an exception if an error has been raised.
*/
private void checkEglError(string msg)
{
int error;
if ((error = EGL14.EglGetError()) != EGL14.EglSuccess)
{
throw new Exception(msg + ": EGL error: 0x" + error.ToString());
}
}
}
}