Project

General

Profile

« Previous | Next » 

Revision bc0a685b

Added by Leszek Koltunski almost 8 years ago

Add License

View differences:

src/main/java/org/distorted/examples/plainmonalisa/EglCore.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
1 20
package org.distorted.examples.plainmonalisa;
2 21

  
3 22
import android.graphics.SurfaceTexture;
......
11 30
import android.view.Surface;
12 31

  
13 32
///////////////////////////////////////////////////////////////////////////////////////////////////
14
/**
15
 * Core EGL state (display, context, config).
16
 * <p>
17
 * The EGLContext must only be attached to one thread at a time.  This class is not thread-safe.
18
 */
33

  
19 34
public final class EglCore
20 35
  {
21 36
  private static final String TAG = "EglCore";
22 37

  
23
  /**
24
   * Constructor flag: surface must be recordable.  This discourages EGL from using a
25
   * pixel format that cannot be converted efficiently to something usable by the video
26
   * encoder.
27
   */
28
  public static final int FLAG_RECORDABLE = 0x01;
29

  
30
  /**
31
   * Constructor flag: ask for GLES3, fall back to GLES2 if not available.  Without this
32
   * flag, GLES2 is used.
33
   */
34
  public static final int FLAG_TRY_GLES3 = 0x02;
38
  public static final int FLAG_RECORDABLE = 0x01;           // Surface must be recordable.  This discourages EGL from using a
39
                                                            // pixel format that cannot be converted efficiently to something 
40
                                                            // usable by the video encoder.
41
  public static final int FLAG_TRY_GLES3 = 0x02;            // Ask for GLES3, fall back to GLES2 if not available.
35 42

  
36
  // Android-specific extension.
37
  private static final int EGL_RECORDABLE_ANDROID = 0x3142;
43
  private static final int EGL_RECORDABLE_ANDROID = 0x3142; // Android-specific extension
38 44

  
39 45
  private EGLDisplay mEGLDisplay = EGL14.EGL_NO_DISPLAY;
40 46
  private EGLContext mEGLContext = EGL14.EGL_NO_CONTEXT;
......
42 48
  private int mGlVersion = -1;
43 49

  
44 50
///////////////////////////////////////////////////////////////////////////////////////////////////
45
  /**
46
   * Prepares EGL display and context.
47
   * <p>
48
   * Equivalent to EglCore(null, 0).
49
   */
51

  
50 52
  public EglCore()
51 53
    {
52 54
    this(null, 0);
53 55
    }
54 56

  
55 57
///////////////////////////////////////////////////////////////////////////////////////////////////
56
  /**
57
   * Prepares EGL display and context.
58
   * <p>
59
   * @param sharedContext The context to share, or null if sharing is not desired.
60
   * @param flags Configuration bit flags, e.g. FLAG_RECORDABLE.
61
   */
58

  
62 59
  public EglCore(EGLContext sharedContext, int flags)
63 60
    {
64 61
    if (mEGLDisplay != EGL14.EGL_NO_DISPLAY)
......
132 129
    }
133 130

  
134 131
///////////////////////////////////////////////////////////////////////////////////////////////////
135
  /**
136
   * Finds a suitable EGLConfig.
137
   *
138
   * @param flags Bit flags from constructor.
139
   * @param version Must be 2 or 3.
140
   */
132

  
141 133
  private EGLConfig getConfig(int flags, int version)
142 134
    {
143 135
    int renderableType = EGL14.EGL_OPENGL_ES2_BIT;
......
182 174
    }
183 175

  
184 176
///////////////////////////////////////////////////////////////////////////////////////////////////
185
  /**
186
   * Discards all resources held by this class, notably the EGL context.  This must be
187
   * called from the thread where the context was created.
188
   * <p>
189
   * On completion, no context will be current.
190
   */
177

  
191 178
  public void release()
192 179
    {
193 180
    if (mEGLDisplay != EGL14.EGL_NO_DISPLAY)
......
226 213
    }
227 214

  
228 215
///////////////////////////////////////////////////////////////////////////////////////////////////
229
  /**
230
   * Destroys the specified surface.  Note the EGLSurface won't actually be destroyed if it's
231
   * still current in a context.
232
   */
216

  
233 217
  public void releaseSurface(EGLSurface eglSurface)
234 218
    {
235 219
    EGL14.eglDestroySurface(mEGLDisplay, eglSurface);
236 220
    }
237 221

  
238 222
///////////////////////////////////////////////////////////////////////////////////////////////////
239
  /**
240
   * Creates an EGL surface associated with a Surface.
241
   * <p>
242
   * If this is destined for MediaCodec, the EGLConfig should have the "recordable" attribute.
243
   */
223

  
244 224
  public EGLSurface createWindowSurface(Object surface)
245 225
    {
246 226
    if (!(surface instanceof Surface) && !(surface instanceof SurfaceTexture))
......
248 228
      throw new RuntimeException("invalid surface: " + surface);
249 229
      }
250 230

  
251
    // Create a window surface, and attach it to the Surface we received.
252 231
    int[] surfaceAttribs = { EGL14.EGL_NONE };
253 232
    EGLSurface eglSurface = EGL14.eglCreateWindowSurface(mEGLDisplay, mEGLConfig, surface, surfaceAttribs, 0);
254 233
    checkEglError("eglCreateWindowSurface");
......
261 240
    }
262 241

  
263 242
///////////////////////////////////////////////////////////////////////////////////////////////////
264
  /**
265
   * Makes our EGL context current, using the supplied surface for both "draw" and "read".
266
   */
243

  
267 244
  public void makeCurrent(EGLSurface eglSurface)
268 245
    {
269 246
    if (mEGLDisplay == EGL14.EGL_NO_DISPLAY)
270 247
      {
271
      // called makeCurrent() before create?
272 248
      Log.d(TAG, "NOTE: makeCurrent w/o display");
273 249
      }
274 250
    if (!EGL14.eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext))
......
278 254
    }
279 255

  
280 256
///////////////////////////////////////////////////////////////////////////////////////////////////
281
  /**
282
   * Makes no context current.
283
   */
257

  
284 258
  public void makeNothingCurrent()
285 259
    {
286 260
    if (!EGL14.eglMakeCurrent(mEGLDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT))
......
290 264
    }
291 265

  
292 266
///////////////////////////////////////////////////////////////////////////////////////////////////
293
  /**
294
   * Returns the GLES version this context is configured for (currently 2 or 3).
295
   */
267

  
296 268
  public int getGlVersion()
297 269
    {
298 270
    return mGlVersion;
299 271
    }
300 272

  
301 273
///////////////////////////////////////////////////////////////////////////////////////////////////
302
  /**
303
   * Calls eglSwapBuffers.  Use this to "publish" the current frame.
304
   *
305
   * @return false on failure
306
   */
274

  
307 275
  public boolean swapBuffers(EGLSurface eglSurface)
308 276
    {
309 277
    return EGL14.eglSwapBuffers(mEGLDisplay, eglSurface);
310 278
    }
311 279

  
312 280
///////////////////////////////////////////////////////////////////////////////////////////////////
313
  /**
314
   * Checks for EGL errors.  Throws an exception if an error has been raised.
315
   */
281

  
316 282
  private void checkEglError(String msg)
317 283
    {
318 284
    int error;
......
323 289
      }
324 290
    }
325 291
  }
326
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff