GLSurfaceView的简单分析及巧妙借用_seteglcontextfactory-程序员宅基地

技术标签: android  video  

https://blog.csdn.net/junzia/article/details/73717506

 

GLSurfaceView的简单分析及巧妙借用

2017年06月25日 14:59:39 湖广午王 阅读数:10582 标签: android opengl es 更多

个人分类: OpenGLES Android 午王

所属专栏: Android OpenGLES

版权声明:欢迎转载,转载请保留文章出处。 https://blog.csdn.net/junzia/article/details/73717506

学习在Android中使用OpenGLES,就不得不提到一个控件:GLSurfaceView。本篇博客将介绍GLSurfaceView的基本使用、GLSurfaceView的源码的简单分析,以及使用GLSurfaceView渲染图像到SurfaceView/TextureView或者PBuffer上等。

关于GLSurfaceView

GLSurfaceView继承了SurfaceView,实现了SurfaceHolder.Callback2接口。
SurfaceView在View的基础上是创建了独立的Surface,拥有SurfaceHolder来管理它的Surface,渲染的工作可以不再在主线程中做了。可以通过SurfaceHolder得到Canvas,在单独的线程中,利用Canvas绘制需要显示的内容,然后更新到Surface上。
而GLSurfaceView,它主要是在SurfaceView的基础上实现了一个GLThread(EGLContext创建GL环境所在线程即为GL线程),绘制的工作直接通过OpenGL来进行,绘制的内容默认情况下依旧是绘制到SurfaceView所提供的Surface上。
参照GLSurfaceView的实现,我们可以自行创建GL环境,来进行GL渲染。实现自行制定指定载体、后台渲染等功能。

GLSurfaceView的使用

GLSurfaceView作为一个View,实例化上基本和其他View相当。可以选择在xml布局文件中增加,然后在Java代码中取得它的控制权。也可以在Java代码中直接new,然后呈现出来。GLSurfaceView必须加入到一个布局中,才能正确的使用,否则有可能会造成崩溃
,这和GLSurfaceView的attachToWindow和detachFromWindow中相关操作。
GLSurfaceView具有onResume和onPause两个同Activity及Fragment中的生命周期同名的方法。一般来说,在Activity或者Fragment中的onResume和onPause方法中,需要主动调用GLSurfaceView的实例的这两个方法。
GLSurfaceView的基本使用代码如下:

mGLView= (GLSurfaceView) findViewById(R.id.mGLView);
//GLContext设置为OpenGLES2.0
mGLView.setEGLContextClientVersion(2);
//在setRenderer之前,可以调用以下方法来进行EGL设置
//mGLView.setEGLConfigChooser();    //颜色、深度、模板等等设置
//mGLView.setEGLWindowSurfaceFactory(); //窗口设置
//mGLView.setEGLContextFactory();   //EGLContext设置
//设置渲染器,渲染主要就是由渲染器来决定
mGLView.setRenderer(new GLSurfaceView.Renderer(){

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        //todo surface被创建后需要做的处理
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        //todo 渲染窗口大小发生改变的处理
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        //todo 执行渲染工作
    }
});
/*渲染方式,RENDERMODE_WHEN_DIRTY表示被动渲染,只有在调用requestRender或者onResume等方法时才会进行渲染。RENDERMODE_CONTINUOUSLY表示持续渲染*/
mGLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
  • 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

GLSurfaceView源码分析

GLSurfaceView中,对于GL环境的操作,出queueEvent是将事件放入队列中,到GL线程中执行外,其他方法基本都是在主线程(也可以是其他线程,非当前GLSurfaceView实例的GL线程)中修改某个状态值,然后取消GL线程的等待,在GL线程中根据状态值作相应的操作,并在操作后反馈给调用方法的那个线程,当然有的方法也不需要反馈。
GLSurfaceView主要方法及中文注释如下:

public class GLSurfaceView extends SurfaceView implements SurfaceHolder.Callback2 {

    public final static int RENDERMODE_WHEN_DIRTY = 0;
    public final static int RENDERMODE_CONTINUOUSLY = 1;

    public GLSurfaceView(Context context) {
        super(context);
        init();
    }

    public GLSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    //调试用的
    public void setDebugFlags(int debugFlags);
    public int getDebugFlags();

    //设置暂停的时候是否保持EglContext
    public void setPreserveEGLContextOnPause(boolean preserveOnPause);
    public boolean getPreserveEGLContextOnPause();

    //设置渲染器,这个非常重要,渲染工作就依靠渲染器了
    //调用此方法会开启一个新的线程,即GL线程
    public void setRenderer(Renderer renderer) {
        checkRenderThreadState();
        if (mEGLConfigChooser == null) {
            mEGLConfigChooser = new SimpleEGLConfigChooser(true);
        }
        if (mEGLContextFactory == null) {
            mEGLContextFactory = new DefaultContextFactory();
        }
        if (mEGLWindowSurfaceFactory == null) {
            mEGLWindowSurfaceFactory = new DefaultWindowSurfaceFactory();
        }
        mRenderer = renderer;
        mGLThread = new GLThread(mThisWeakRef);
        mGLThread.start();
    }

    //设置EGLContext工厂,不设置就用默认的
    public void setEGLContextFactory(EGLContextFactory factory);

    //设置EGLSurface工厂,不设置就用默认的
    public void setEGLWindowSurfaceFactory(EGLWindowSurfaceFactory factory);

    //设置EglConfig,一般颜色深度等等,利用此方法设置。不设置就用默认的
    public void setEGLConfigChooser(EGLConfigChooser configChooser);

    //内部调用setEGLConfigChooser
    public void setEGLConfigChooser(boolean needDepth);

    //内部调用setEGLConfigChooser
    public void setEGLConfigChooser(int redSize, int greenSize, int blueSize,
            int alphaSize, int depthSize, int stencilSize);

    //设置EGLContextVersion,比如2,即OpenGLES2.0
    public void setEGLContextClientVersion(int version);

    //设置渲染方式,有RENDERMODE_CONTINUOUSLY表示不断渲染
    //以及RENDERMODE_WHEN_DIRTY表示在需要的时候才会渲染
    //渲染的时候要求调用requestRender,必须在setRenderer后调用
    public void setRenderMode(int renderMode);

    public int getRenderMode();

    //主动请求渲染
    public void requestRender();


    public void surfaceCreated(SurfaceHolder holder);

    public void surfaceDestroyed(SurfaceHolder holder);

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h);

    @Override
    public void surfaceRedrawNeeded(SurfaceHolder holder) {
        if (mGLThread != null) {
            mGLThread.requestRenderAndWait();
        }
    }

    //生命周期,一般在Activity、Fragment的onPause中调用
    public void onPause();

    //生命周期,一般在Activity、Fragment的onResume中调用
    public void onResume();

    //向GL线程发送一个任务
    public void queueEvent(Runnable r);

    //附加到Window上时被调用,外部不可调用
    protected void onAttachedToWindow();

     //从Window上被移除时调用,外部不可调用
    protected void onDetachedFromWindow();

    //渲染器接口
    public interface Renderer {
        //Surface被创建时被调用,通常在此进行渲染的初始化
        void onSurfaceCreated(GL10 gl, EGLConfig config);
        //Surface大小被改变时被调用
        void onSurfaceChanged(GL10 gl, int width, int height);
        //执行渲染时被调用,以完成用户渲染工作
        void onDrawFrame(GL10 gl);
    }

    //非常重要的一个EGL帮助类,GL环境的建立依靠此类
    private static class EglHelper {
        public EglHelper(WeakReference<GLSurfaceView> glSurfaceViewWeakRef) {
            mGLSurfaceViewWeakRef = glSurfaceViewWeakRef;
        }

        //EGL的初始化,可以参考此方法
        public void start() {
            if (LOG_EGL) {
                Log.w("EglHelper", "start() tid=" + Thread.currentThread().getId());
            }
            /*
             * Get an EGL instance
             */
            mEgl = (EGL10) EGLContext.getEGL();

            /*
             * Get to the default display.
             */
            mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

            if (mEglDisplay == EGL10.EGL_NO_DISPLAY) {
                throw new RuntimeException("eglGetDisplay failed");
            }

            /*
             * We can now initialize EGL for that display
             */
            int[] version = new int[2];
            if(!mEgl.eglInitialize(mEglDisplay, version)) {
                throw new RuntimeException("eglInitialize failed");
            }
            GLSurfaceView view = mGLSurfaceViewWeakRef.get();
            if (view == null) {
                mEglConfig = null;
                mEglContext = null;
            } else {
                mEglConfig = view.mEGLConfigChooser.chooseConfig(mEgl, mEglDisplay);

                /*
                * Create an EGL context. We want to do this as rarely as we can, because an
                * EGL context is a somewhat heavy object.
                */
                mEglContext = view.mEGLContextFactory.createContext(mEgl, mEglDisplay, mEglConfig);
            }
            if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) {
                mEglContext = null;
                throwEglException("createContext");
            }
            if (LOG_EGL) {
                Log.w("EglHelper", "createContext " + mEglContext + " tid=" + Thread.currentThread().getId());
            }

            mEglSurface = null;
        }

        //创建EGLSurface,使GL的渲染,能够渲染到用户指定的Surface
        //默认的Surface就是SurfaceHolder的Surface
        public boolean createSurface() {
            if (LOG_EGL) {
                Log.w("EglHelper", "createSurface()  tid=" + Thread.currentThread().getId());
            }
            /*
             * Check preconditions.
             */
            if (mEgl == null) {
                throw new RuntimeException("egl not initialized");
            }
            if (mEglDisplay == null) {
                throw new RuntimeException("eglDisplay not initialized");
            }
            if (mEglConfig == null) {
                throw new RuntimeException("mEglConfig not initialized");
            }

            /*
             *  The window size has changed, so we need to create a new
             *  surface.
             */
            destroySurfaceImp();

            /*
             * Create an EGL surface we can render into.
             */
            GLSurfaceView view = mGLSurfaceViewWeakRef.get();
            if (view != null) {
                mEglSurface = view.mEGLWindowSurfaceFactory.createWindowSurface(mEgl,
                        mEglDisplay, mEglConfig, view.getHolder());
            } else {
                mEglSurface = null;
            }

            if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
                int error = mEgl.eglGetError();
                if (error == EGL10.EGL_BAD_NATIVE_WINDOW) {
                    Log.e("EglHelper", "createWindowSurface returned EGL_BAD_NATIVE_WINDOW.");
                }
                return false;
            }

            /*
             * Before we can issue GL commands, we need to make sure
             * the context is current and bound to a surface.
             */
            if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
                /*
                 * Could not make the context current, probably because the underlying
                 * SurfaceView surface has been destroyed.
                 */
                logEglErrorAsWarning("EGLHelper", "eglMakeCurrent", mEgl.eglGetError());
                return false;
            }

            return true;
        }

        //通过EGL得到GL,然后用户设置了Wrapper的话会给得到的GL做个包装
        //同时也会解析一下用户的Debug意图,看看要不要debug
        GL createGL();

        //绘制完成之后,调用此方法,将绘制的内容输出到前台,让用户可以看到
        public int swap();

        //销毁Surface的方法,具体实现在destroySurfaceImp方法中
        public void destroySurface();

        //销毁GL环境
        public void finish();

    }

    //GL线程,此类中存在的方法,GLSurfaceView中有同名的,
    //基本都是提供给GLSurfaceView作为真正的实现调用
    static class GLThread extends Thread {

        //销毁EglSurface
        private void stopEglSurfaceLocked();

        //销毁EglContext
        private void stopEglContextLocked();

        //GL线程的主要逻辑都在这个方法里面,这个方法比较复杂
        //GLSurfaceView的核心就在这个里面了,最后在单独分析这个里面的逻辑
        private void guardedRun() throws InterruptedException;

        public boolean ableToDraw() {
            return mHaveEglContext && mHaveEglSurface && readyToDraw();
        }

        private boolean readyToDraw() {
            return (!mPaused) && mHasSurface && (!mSurfaceIsBad)
                && (mWidth > 0) && (mHeight > 0)
                && (mRequestRender || (mRenderMode == RENDERMODE_CONTINUOUSLY));
        }

        //设置渲染方法,见GLSurfaceView的setRenderMode
        public void setRenderMode(int renderMode);

        public int getRenderMode();

        //请求一次渲染
        public void requestRender();

        //请求一次渲染,并等待渲染完成
        public void requestRenderAndWait();

        //创建Surface
        public void surfaceCreated();

        //销毁Surface
        public void surfaceDestroyed();

        public void onPause();
        public void onResume();

        //Surface的大小被改变时调用
        public void onWindowResize(int w, int h);

        //请求退出渲染线程,并等待退出
        public void requestExitAndWait();

        //请求是否EglContext
        public void requestReleaseEglContextLocked();

        //向GL线程发送一个任务
        public void queueEvent(Runnable r);

    }

    //debug使用的
    static class LogWriter extends Writer {

    }


    //很多方法都会调用此方法,会检查mGLThread不为null
    //即保证调用此方法的方法,必须在setRenderer之前调用
    private void checkRenderThreadState();

    //主要就是用来做同步用的,利用Object的wait和notifyAll
    private static class GLThreadManager {

    }

}
  • 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

渲染的主要逻辑在GLThread的guardedRun()方法中,对guardedRun()方法梳理下。

private void guardedRun() throws InterruptedException {
    mEglHelper = new EglHelper(mGLSurfaceViewWeakRef);
    mHaveEglContext = false;
    mHaveEglSurface = false;
    mWantRenderNotification = false;

    try {
        GL10 gl = null;
        boolean createEglContext = false;
        boolean createEglSurface = false;
        boolean createGlInterface = false;
        boolean lostEglContext = false;
        boolean sizeChanged = false;
        boolean wantRenderNotification = false;
        boolean doRenderNotification = false;
        boolean askedToReleaseEglContext = false;
        int w = 0;
        int h = 0;
        Runnable event = null;

        while (true) {
            synchronized (sGLThreadManager) {
                while (true) {
                    //外部请求退出GL线程
                    if (mShouldExit) {
                        return;
                    }

                    /*外部请求在GL线程中处理的事件没有处理完时,就优先处理这些事件*/
                    if (! mEventQueue.isEmpty()) {
                        event = mEventQueue.remove(0);
                        break;
                    }

                    //暂停和恢复状态变化时,onResume和onPause状态变化
                    boolean pausing = false;
                    if (mPaused != mRequestPaused) {
                        pausing = mRequestPaused;
                        mPaused = mRequestPaused;
                        /*GLSurfaceView的onPause和onResume都会用wait方法等待GL线程的响应,这时候主线程阻塞。此处调用notifyAll通知onPause和onResume,放弃主线程的阻塞。GLSurfaceView中其他很多方法也存在wait方法,基本与此类似
                        */
                        sGLThreadManager.notifyAll();
                        if (LOG_PAUSE_RESUME) {
                            Log.i("GLThread", "mPaused is now " + mPaused + " tid=" + getId());
                        }
                    }

                    // 需要释放EglContext时候执行的工作
                    if (mShouldReleaseEglContext) {
                        if (LOG_SURFACE) {
                            Log.i("GLThread", "releasing EGL context because asked to tid=" + getId());
                        }
                        stopEglSurfaceLocked();
                        stopEglContextLocked();
                        mShouldReleaseEglContext = false;
                        askedToReleaseEglContext = true;
                    }

                    // EglContext丢失时,销毁EglSurface和EglContext
                    if (lostEglContext) {
                        stopEglSurfaceLocked();
                        stopEglContextLocked();
                        lostEglContext = false;
                    }

                    //接收了暂停信号,而且当前EglSurface存在时,销毁EglSurface
                    if (pausing && mHaveEglSurface) {
                        if (LOG_SURFACE) {
                            Log.i("GLThread", "releasing EGL surface because paused tid=" + getId());
                        }
                        stopEglSurfaceLocked();
                    }

                    /*接收了暂停信号,而且当前EglContext存在时,根据用户设置,来决定是否销毁EglContext*/
                    if (pausing && mHaveEglContext) {
                        GLSurfaceView view = mGLSurfaceViewWeakRef.get();
                        boolean preserveEglContextOnPause = view == null ?
                                false : view.mPreserveEGLContextOnPause;
                        if (!preserveEglContextOnPause) {
                            stopEglContextLocked();
                            if (LOG_SURFACE) {
                                Log.i("GLThread", "releasing EGL context because paused tid=" + getId());
                            }
                        }
                    }

                    /*Surface不存在(不是EglSurface),而且当前并没有在等待Surface*/
                    if ((! mHasSurface) && (! mWaitingForSurface)) {
                        if (LOG_SURFACE) {
                            Log.i("GLThread", "noticed surfaceView surface lost tid=" + getId());
                        }
                        if (mHaveEglSurface) {
                            stopEglSurfaceLocked();
                        }
                        mWaitingForSurface = true;
                        mSurfaceIsBad = false;
                        sGLThreadManager.notifyAll();
                    }

                    // Surface存在,而且在等待Surface
                    if (mHasSurface && mWaitingForSurface) {
                        if (LOG_SURFACE) {
                            Log.i("GLThread", "noticed surfaceView surface acquired tid=" + getId());
                        }
                        mWaitingForSurface = false;
                        sGLThreadManager.notifyAll();
                    }

                    if (doRenderNotification) {
                        if (LOG_SURFACE) {
                            Log.i("GLThread", "sending render notification tid=" + getId());
                        }
                        mWantRenderNotification = false;
                        doRenderNotification = false;
                        mRenderComplete = true;
                        sGLThreadManager.notifyAll();
                    }

                    // 当前环境准备好了渲染执行,否则进入下一轮等待及判断
                    if (readyToDraw()) {

                        // 没有EglContext就需要借助EglHelper来创建EglContext
                        if (! mHaveEglContext) {
                            if (askedToReleaseEglContext) {
                                askedToReleaseEglContext = false;
                            } else {
                                try {
                                    mEglHelper.start();
                                } catch (RuntimeException t) {
                                    sGLThreadManager.releaseEglContextLocked(this);
                                    throw t;
                                }
                                mHaveEglContext = true;
                                createEglContext = true;

                                sGLThreadManager.notifyAll();
                            }
                        }

                        /*有了EglContext,但是没有EglSurface,就需要设置一些状态,以便后续操作*/
                        if (mHaveEglContext && !mHaveEglSurface) {
                            mHaveEglSurface = true;
                            createEglSurface = true;
                            createGlInterface = true;
                            sizeChanged = true;
                        }

                        /*有eglSurface时,需要判断是否需要执行surface sizechange*/
                        if (mHaveEglSurface) {
                            if (mSizeChanged) {
                                sizeChanged = true;
                                w = mWidth;
                                h = mHeight;
                                mWantRenderNotification = true;
                                if (LOG_SURFACE) {
                                    Log.i("GLThread",
                                            "noticing that we want render notification tid="
                                                    + getId());
                                }

                                // Destroy and recreate the EGL surface.
                                createEglSurface = true;

                                mSizeChanged = false;
                            }
                            mRequestRender = false;
                            sGLThreadManager.notifyAll();
                            if (mWantRenderNotification) {
                                wantRenderNotification = true;
                            }
                            //注意此处break,跳出等待的循环
                            break;
                        }
                    }

                    // By design, this is the only place in a GLThread thread where we wait().
                    if (LOG_THREADS) {
                        Log.i("GLThread", "waiting tid=" + getId()
                                + " mHaveEglContext: " + mHaveEglContext
                                + " mHaveEglSurface: " + mHaveEglSurface
                                + " mFinishedCreatingEglSurface: " + mFinishedCreatingEglSurface
                                + " mPaused: " + mPaused
                                + " mHasSurface: " + mHasSurface
                                + " mSurfaceIsBad: " + mSurfaceIsBad
                                + " mWaitingForSurface: " + mWaitingForSurface
                                + " mWidth: " + mWidth
                                + " mHeight: " + mHeight
                                + " mRequestRender: " + mRequestRender
                                + " mRenderMode: " + mRenderMode);
                    }
                    sGLThreadManager.wait();
                }
            } 

            /*外部请求在GL线程中处理的事件没有处理完时,就优先处理这些事件*/
            if (event != null) {
                event.run();
                event = null;
                continue;
            }


            //后续就是根据上面的判断设置,来执行相应的操作
            if (createEglSurface) {
                if (LOG_SURFACE) {
                    Log.w("GLThread", "egl createSurface");
                }
                //创建EglSurface
                if (mEglHelper.createSurface()) {       
                    synchronized(sGLThreadManager) {
                        mFinishedCreatingEglSurface = true;
                        sGLThreadManager.notifyAll();
                    }
                } else {
                    synchronized(sGLThreadManager) {
                        mFinishedCreatingEglSurface = true;
                        mSurfaceIsBad = true;
                        sGLThreadManager.notifyAll();
                    }
                    continue;
                }
                createEglSurface = false;
            }

            if (createGlInterface) {
                gl = (GL10) mEglHelper.createGL();

                createGlInterface = false;
            }

            if (createEglContext) {
                if (LOG_RENDERER) {
                    Log.w("GLThread", "onSurfaceCreated");
                }
                GLSurfaceView view = mGLSurfaceViewWeakRef.get();
                if (view != null) {
                    try {
                        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "onSurfaceCreated");
                        //调用GLSurfaceView设置的renderer的onSurfceCreated方法
                        view.mRenderer.onSurfaceCreated(gl, mEglHelper.mEglConfig);
                    } finally {
                        Trace.traceEnd(Trace.TRACE_TAG_VIEW);
                    }
                }
                createEglContext = false;
            }

            //surface大小被改变
            if (sizeChanged) {
                if (LOG_RENDERER) {
                    Log.w("GLThread", "onSurfaceChanged(" + w + ", " + h + ")");
                }
                GLSurfaceView view = mGLSurfaceViewWeakRef.get();
                if (view != null) {
                    try {
                        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "onSurfaceChanged");
                        view.mRenderer.onSurfaceChanged(gl, w, h);
                    } finally {
                        Trace.traceEnd(Trace.TRACE_TAG_VIEW);
                    }
                }
                sizeChanged = false;
            }

            if (LOG_RENDERER_DRAW_FRAME) {
                Log.w("GLThread", "onDrawFrame tid=" + getId());
            }
            //每帧绘制
            {
                GLSurfaceView view = mGLSurfaceViewWeakRef.get();
                if (view != null) {
                    try {
                        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "onDrawFrame");
                        view.mRenderer.onDrawFrame(gl);
                    } finally {
                        Trace.traceEnd(Trace.TRACE_TAG_VIEW);
                    }
                }
            }
            int swapError = mEglHelper.swap();
            switch (swapError) {
                case EGL10.EGL_SUCCESS:
                    break;
                case EGL11.EGL_CONTEXT_LOST:
                    if (LOG_SURFACE) {
                        Log.i("GLThread", "egl context lost tid=" + getId());
                    }
                    lostEglContext = true;
                    break;
                default:
                    EglHelper.logEglErrorAsWarning("GLThread", "eglSwapBuffers", swapError);

                    synchronized(sGLThreadManager) {
                        mSurfaceIsBad = true;
                        sGLThreadManager.notifyAll();
                    }
                    break;
            }

            if (wantRenderNotification) {
                doRenderNotification = true;
                wantRenderNotification = false;
            }
        }

    } finally {
            /*
             * clean-up everything...
             */
        synchronized (sGLThreadManager) {
            stopEglSurfaceLocked();
            stopEglContextLocked();
        }
    }
}
  • 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

SurfaceView/TextureView或Pbuffer上的OpenGLES渲染

根据上面的分析,我们知道,GLSurfaceView有setEGLWindowSurfaceFactory借助此方法,我们可以将图像渲染到其他的地方,比如我们创建一个如下的自定义GLSurfaceView,就可以将图像渲染到外部指定surface上。但是遗憾的是,在某些手机上,这种方式会失效。

private class GLView extends GLSurfaceView{

    public GLView(Context context) {
        super(context);
        init();
    }

    private void init(){
        getHolder().addCallback(null);
        setEGLWindowSurfaceFactory(new GLSurfaceView.EGLWindowSurfaceFactory() {
            @Override
            public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display, EGLConfig
                config, Object window) {
                return egl.eglCreateWindowSurface(display,config,surface,null);
            }

            @Override
            public void destroySurface(EGL10 egl, EGLDisplay display, EGLSurface surface) {
                egl.eglDestroySurface(display, surface);
            }
        });
        setEGLContextClientVersion(2);
        setRenderer(TextureController.this);
        setRenderMode(RENDERMODE_WHEN_DIRTY);
        setPreserveEGLContextOnPause(true);
    }

    public void attachedToWindow(){
        super.onAttachedToWindow();
    }

    public void detachedFromWindow(){
        super.onDetachedFromWindow();
    }

}
  • 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

那么如何办呢?我们可以新建一个类GLEnvironment,将GLSurfaceView内容全部复制出来,然后取消其继承和接口实现,将所有报错的代码删除掉,这样就相当于剔除了GLSurfaceView的SurfaceView而保留了它的GL环境,我们可以使用GLEnvironment来进行渲染,并自由的指定渲染载体,可以是SurfaceView/TextureView或Pbuffer,也可以是Pixmap。
同样是利用setEGLWindowSurfaceFactory方法来设置,当然可以改个名字更为贴切,比如setEGLSurfaceFactory,如下:

mEnv.setEGLSurfaceFactory(new GLEnvironment.EGLSurfaceFactory() {
    @Override
    public EGLSurface createSurface(EGL10 egl, EGLDisplay display, EGLConfig config, Object nativeWindow) {
        /*使用SurfaceView或者TextureView,customWindow可以为SurfaceTexture\SurfaceHolder或者Surface等*/
        egl.eglCreateWindowSurface(display,config,customWindow,null);
        //使用pbuffer
        //reture egl.eglCreatePbufferSurface();
        //使用pixmap
        //return egl.eglCreatePixmapSurface();
    }

    @Override
    public void destroySurface(EGL10 egl, EGLDisplay display, EGLSurface surface) {
         egl.eglDestroySurface(display,surface);
    }
});
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010029439/article/details/85542382

智能推荐

从零开始搭建Hadoop_创建一个hadoop项目-程序员宅基地

文章浏览阅读331次。第一部分:准备工作1 安装虚拟机2 安装centos73 安装JDK以上三步是准备工作,至此已经完成一台已安装JDK的主机第二部分:准备3台虚拟机以下所有工作最好都在root权限下操作1 克隆上面已经有一台虚拟机了,现在对master进行克隆,克隆出另外2台子机;1.1 进行克隆21.2 下一步1.3 下一步1.4 下一步1.5 根据子机需要,命名和安装路径1.6 ..._创建一个hadoop项目

心脏滴血漏洞HeartBleed CVE-2014-0160深入代码层面的分析_heartbleed代码分析-程序员宅基地

文章浏览阅读1.7k次。心脏滴血漏洞HeartBleed CVE-2014-0160 是由heartbeat功能引入的,本文从深入码层面的分析该漏洞产生的原因_heartbleed代码分析

java读取ofd文档内容_ofd电子文档内容分析工具(分析文档、签章和证书)-程序员宅基地

文章浏览阅读1.4k次。前言ofd是国家文档标准,其对标的文档格式是pdf。ofd文档是容器格式文件,ofd其实就是压缩包。将ofd文件后缀改为.zip,解压后可看到文件包含的内容。ofd文件分析工具下载:点我下载。ofd文件解压后,可以看到如下内容: 对于xml文件,可以用文本工具查看。但是对于印章文件(Seal.esl)、签名文件(SignedValue.dat)就无法查看其内容了。本人开发一款ofd内容查看器,..._signedvalue.dat

基于FPGA的数据采集系统(一)_基于fpga的信息采集-程序员宅基地

文章浏览阅读1.8w次,点赞29次,收藏313次。整体系统设计本设计主要是对ADC和DAC的使用,主要实现功能流程为:首先通过串口向FPGA发送控制信号,控制DAC芯片tlv5618进行DA装换,转换的数据存在ROM中,转换开始时读取ROM中数据进行读取转换。其次用按键控制adc128s052进行模数转换100次,模数转换数据存储到FIFO中,再从FIFO中读取数据通过串口输出显示在pc上。其整体系统框图如下:图1:FPGA数据采集系统框图从图中可以看出,该系统主要包括9个模块:串口接收模块、按键消抖模块、按键控制模块、ROM模块、D.._基于fpga的信息采集

微服务 spring cloud zuul com.netflix.zuul.exception.ZuulException GENERAL-程序员宅基地

文章浏览阅读2.5w次。1.背景错误信息:-- [http-nio-9904-exec-5] o.s.c.n.z.filters.post.SendErrorFilter : Error during filteringcom.netflix.zuul.exception.ZuulException: Forwarding error at org.springframework.cloud..._com.netflix.zuul.exception.zuulexception

邻接矩阵-建立图-程序员宅基地

文章浏览阅读358次。1.介绍图的相关概念  图是由顶点的有穷非空集和一个描述顶点之间关系-边(或者弧)的集合组成。通常,图中的数据元素被称为顶点,顶点间的关系用边表示,图通常用字母G表示,图的顶点通常用字母V表示,所以图可以定义为:  G=(V,E)其中,V(G)是图中顶点的有穷非空集合,E(G)是V(G)中顶点的边的有穷集合1.1 无向图:图中任意两个顶点构成的边是没有方向的1.2 有向图:图中..._给定一个邻接矩阵未必能够造出一个图

随便推点

MDT2012部署系列之11 WDS安装与配置-程序员宅基地

文章浏览阅读321次。(十二)、WDS服务器安装通过前面的测试我们会发现,每次安装的时候需要加域光盘映像,这是一个比较麻烦的事情,试想一个上万个的公司,你天天带着一个光盘与光驱去给别人装系统,这将是一个多么痛苦的事情啊,有什么方法可以解决这个问题了?答案是肯定的,下面我们就来简单说一下。WDS服务器,它是Windows自带的一个免费的基于系统本身角色的一个功能,它主要提供一种简单、安全的通过网络快速、远程将Window..._doc server2012上通过wds+mdt无人值守部署win11系统.doc

python--xlrd/xlwt/xlutils_xlutils模块可以读xlsx吗-程序员宅基地

文章浏览阅读219次。python–xlrd/xlwt/xlutilsxlrd只能读取,不能改,支持 xlsx和xls 格式xlwt只能改,不能读xlwt只能保存为.xls格式xlutils能将xlrd.Book转为xlwt.Workbook,从而得以在现有xls的基础上修改数据,并创建一个新的xls,实现修改xlrd打开文件import xlrdexcel=xlrd.open_workbook('E:/test.xlsx') 返回值为xlrd.book.Book对象,不能修改获取sheett_xlutils模块可以读xlsx吗

关于新版本selenium定位元素报错:‘WebDriver‘ object has no attribute ‘find_element_by_id‘等问题_unresolved attribute reference 'find_element_by_id-程序员宅基地

文章浏览阅读8.2w次,点赞267次,收藏656次。运行Selenium出现'WebDriver' object has no attribute 'find_element_by_id'或AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'等定位元素代码错误,是因为selenium更新到了新的版本,以前的一些语法经过改动。..............._unresolved attribute reference 'find_element_by_id' for class 'webdriver

DOM对象转换成jQuery对象转换与子页面获取父页面DOM对象-程序员宅基地

文章浏览阅读198次。一:模态窗口//父页面JSwindow.showModalDialog(ifrmehref, window, 'dialogWidth:550px;dialogHeight:150px;help:no;resizable:no;status:no');//子页面获取父页面DOM对象//window.showModalDialog的DOM对象var v=parentWin..._jquery获取父window下的dom对象

什么是算法?-程序员宅基地

文章浏览阅读1.7w次,点赞15次,收藏129次。算法(algorithm)是解决一系列问题的清晰指令,也就是,能对一定规范的输入,在有限的时间内获得所要求的输出。 简单来说,算法就是解决一个问题的具体方法和步骤。算法是程序的灵 魂。二、算法的特征1.可行性 算法中执行的任何计算步骤都可以分解为基本可执行的操作步,即每个计算步都可以在有限时间里完成(也称之为有效性) 算法的每一步都要有确切的意义,不能有二义性。例如“增加x的值”,并没有说增加多少,计算机就无法执行明确的运算。 _算法

【网络安全】网络安全的标准和规范_网络安全标准规范-程序员宅基地

文章浏览阅读1.5k次,点赞18次,收藏26次。网络安全的标准和规范是网络安全领域的重要组成部分。它们为网络安全提供了技术依据,规定了网络安全的技术要求和操作方式,帮助我们构建安全的网络环境。下面,我们将详细介绍一些主要的网络安全标准和规范,以及它们在实际操作中的应用。_网络安全标准规范