EglHelper.cpp
//
// Created by lenovo on 2019/9/29.
//
#include "../log/AndroidLog.h"
#include "EglHelper.h"
EglHelper::~EglHelper() {
}
EglHelper::EglHelper() {
mEGLContext=EGL_NO_CONTEXT;
mEGLSurface=EGL_NO_SURFACE;
mEGLDisplay=EGL_NO_DISPLAY;
mEGLConfig=NULL;
}
int EglHelper::initEgl(EGLNativeWindowType window) {
mEGLDisplay=eglGetDisplay(EGL_DEFAULT_DISPLAY);
if(mEGLDisplay==EGL_NO_DISPLAY)
{ LOGE("EGLGetDisplay error");
return -1;}
EGLint *version=new EGLint[2];
if(!eglInitialize(mEGLDisplay,&version[0],&version[1]))
{LOGE("EGLInitialize error");
return -1;}
const EGLint attribs[]={
EGL_ALPHA_SIZE,8,
EGL_BLUE_SIZE,8,
EGL_RED_SIZE,8,
EGL_GREEN_SIZE,8,
EGL_DEPTH_SIZE,8,
EGL_STENCIL_SIZE,8,
EGL_RENDERABLE_TYPE,EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLint num_config;
if(!eglChooseConfig(mEGLDisplay,attribs,NULL,1,&num_config))
{
return -1;
}
if(!eglChooseConfig(mEGLDisplay,attribs,&mEGLConfig,num_config,&num_config))
{
return -1;
}
int attrib_list[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
mEGLContext = eglCreateContext(mEGLDisplay, mEGLConfig, EGL_NO_CONTEXT, attrib_list);
if(mEGLContext == EGL_NO_CONTEXT)
{
LOGE("eglCreateContext error");
return -1;
}
if(window == NULL)
{
LOGE("egl window error");
return -1;
}
//6銆?
mEGLSurface = eglCreateWindowSurface(mEGLDisplay, mEGLConfig, window, NULL);
if(mEGLSurface == EGL_NO_SURFACE)
{
LOGE("eglCreateWindowSurface error");
return -1;
}
//7銆?
if(!eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext))
{
LOGE("eglMakeCurrent error");
return -1;
}
LOGD("egl init success! ");
return 0;
}
int EglHelper::swapBuffers() {
if(mEGLDisplay!=EGL_NO_DISPLAY&&mEGLSurface!=EGL_NO_SURFACE)
{
if(eglSwapBuffers(mEGLDisplay, mEGLSurface))
{
return 0;
}
}
return -1;
}
void EglHelper::destoryEgl() {
if(mEGLDisplay != EGL_NO_DISPLAY)
{
eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
}
if(mEGLDisplay != EGL_NO_DISPLAY && mEGLSurface != EGL_NO_SURFACE)
{
eglDestroySurface(mEGLDisplay, mEGLSurface);
mEGLSurface = EGL_NO_SURFACE;
}
if(mEGLDisplay != EGL_NO_DISPLAY && mEGLContext != EGL_NO_CONTEXT){
eglDestroyContext(mEGLDisplay, mEGLContext);
mEGLContext = EGL_NO_CONTEXT;
}
if(mEGLDisplay != EGL_NO_DISPLAY)
{
eglTerminate(mEGLDisplay);
mEGLDisplay = EGL_NO_DISPLAY;
}
}
注意:eglChooseConfig中的参数&eglconfig是给eglconfig设置,不然eglconfig是空,后面创建surface就会失败,屏幕将呈现黑色。
EglHelper.h
//
// Created by lenovo on 2019/9/29.
//
#ifndef OPENGL_MY_EGLHELPER_H
#define OPENGL_MY_EGLHELPER_H
#include <EGL/egl.h>
#include "EglHelper.h"
#include "../log/AndroidLog.h"
class EglHelper {
public:
EGLDisplay mEGLDisplay;
EGLSurface mEGLSurface;
EGLConfig mEGLConfig;
EGLContext mEGLContext;
public:
EglHelper();
~EglHelper();
int initEgl(EGLNativeWindowType win);
int swapBuffers();
void destoryEgl();
};
#endif //OPENGL_MY_EGLHELPER_H
EglThread.cpp
//
// Created by yangw on 2019-2-17.
//
#include "EglThread.h"
EglThread::EglThread() {
}
EglThread::~EglThread() {
}
void * eglThreadImpl(void *context)
{
EglThread *eglThread = static_cast<EglThread *>(context);
if(eglThread != NULL)
{
EglHelper *eglHelper = new EglHelper();
eglHelper->initEgl(eglThread->nativeWindow);
eglThread->isExit = false;
while(true)
{
if(eglThread->isCreate)
{
LOGD("eglthread call surfaceCreate");
eglThread->isCreate = false;
}
if(eglThread->isChange)
{
LOGD("eglthread call surfaceChange");
eglThread->isChange = false;
glViewport(0, 0, 720, 1280);
eglThread->isStart = true;
}
//
LOGD("draw");
if(eglThread->isStart)
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
eglHelper->swapBuffers();
}
usleep(1000000 / 60);
if(eglThread->isExit)
{
break;
}
}
}
// pthread_exit(&wlEglThread->eglThread);
return 0;
}
void EglThread::onSurfaceCreate(EGLNativeWindowType window) {
if(eglThread == -1)
{
isCreate = true;
nativeWindow = window;
pthread_create(&eglThread, NULL, eglThreadImpl, this);
}
}
void EglThread::onSurfaceChange(int width, int height) {
isChange = true;
surfaceWidth = width;
surfaceHeight = height;
}
EglThead.h
//
// Created by lenovo on 2019/9/30.
//
#ifndef OPENGL_MY_EGLTHREAD_H
#define OPENGL_MY_EGLTHREAD_H
#include <EGL/eglplatform.h>
#include "pthread.h"
#include "android/native_window.h"
#include "EglHelper.h"
#include <unistd.h>
#include "GLES2/gl2.h"
class EglThread {
public:
pthread_t eglThread = -1;
bool isCreate = false;
bool isChange = false;
bool isExit = false;
bool isStart = false;
bool surfaceWidth = 0;
bool surfaceHeight = 0;
public:
EglThread();
~EglThread();
void onSurfaceCreate(EGLNativeWindowType window);
void onSurfaceChange(int width, int height);
ANativeWindow *nativeWindow = NULL;
};
#endif //OPENGL_MY_EGLTHREAD_H
native-lib.cpp
#include <jni.h>
#include <string>
#include "EGL/egl.h"
#include "android/native_window.h"
#include "GLES2/gl2.h"
#include "Egl/EglHelper.h"
#include "android/native_window.h"
#include "android/native_window_jni.h"
#include "GLES2/gl2.h"
#include "Egl/EglThread.h"
EglHelper *eglHelper = NULL;
ANativeWindow *nativeWindow = NULL;
EglThread *eglThread=NULL;
extern "C"
JNIEXPORT void JNICALL
Java_com_example_lenovo_opengl_1my_Opengl_NativeOpengl_surfaceCreate(JNIEnv *env,jobject instance,
jobject surface) {
// TODO
nativeWindow = ANativeWindow_fromSurface(env, surface);
eglThread = new EglThread();
eglThread->onSurfaceCreate(nativeWindow);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_lenovo_opengl_1my_Opengl_NativeOpengl_surfaceChange(JNIEnv *env, jobject instance, jint width,
jint height){
// TODO
if(eglThread != NULL)
{
eglThread->onSurfaceChange(width, height);
}
}
mySurfaceView.java
package com.example.lenovo.opengl_my.Opengl;
import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import static android.R.attr.height;
import static android.R.attr.width;
/**
* Created by lenovo on 2019/9/30.
*/
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private NativeOpengl nativeOpengl;
public void setNativeOpengl(NativeOpengl nativeOpengl) {
this.nativeOpengl = nativeOpengl;
}
public MySurfaceView(Context context) {
this(context, null);
}
public MySurfaceView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
if(nativeOpengl != null)
{
nativeOpengl.surfaceCreate(surfaceHolder.getSurface());
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
if(nativeOpengl != null)
{
nativeOpengl.surfaceChange(width, height);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
}
nativeOpengl.java
package com.example.lenovo.opengl_my.Opengl;
import android.view.Surface;
/**
* Created by lenovo on 2019/9/30.
*/
public class NativeOpengl {
static {
System.loadLibrary("native-lib");
}
public native void surfaceCreate(Surface surface);
public native void surfaceChange(int width, int height);
}
mainActivity.java
package com.example.lenovo.opengl_my;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.widget.TextView;
import com.example.lenovo.opengl_my.Opengl.MySurfaceView;
import com.example.lenovo.opengl_my.Opengl.NativeOpengl;
public class MainActivity extends AppCompatActivity {
private MySurfaceView mySurfaceView;
private NativeOpengl nativeOpengl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySurfaceView= (MySurfaceView) findViewById(R.id.surfaceview);
nativeOpengl=new NativeOpengl();
mySurfaceView.setNativeOpengl(nativeOpengl);
}
}