您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
2. IEGL类初始化封装java部分添加XPlay窗口类~1
发布时间:2021-06-10 17:44:37编辑:雪饮阅读()
首先需要初始化窗口cpp/native-lib.cpp:
#include <jni.h>
#include <string>
#include <android/native_window_jni.h>
#include "FFDemux.h"
#include "XLog.h"
#include "FFDecode.h"
#include "XEGL.h"
class TestObs:public IObserver
{
public:
void Update(XData d)
{
//XLOGI("TestObs Update data size is %d",d.size);
}
};
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_example_xplay_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
//XLOGI("S begin!");
//XSleep(3000);
//XLOGI("S end!");
//return env->NewStringUTF(hello.c_str());
///////////////////////////////////
///测试用代码
TestObs *tobs = new TestObs();
IDemux *de = new FFDemux();
//de->AddObs(tobs);
de->Open("/sdcard/1080.mp4");
IDecode *vdecode = new FFDecode();
vdecode->Open(de->GetVPara());
IDecode *adecode = new FFDecode();
adecode->Open(de->GetAPara());
de->AddObs(vdecode);
de->AddObs(adecode);
//vdecode->Open();
de->Start();
vdecode->Start();
adecode->Start();
//XSleep(3000);
//de->Stop();
/*for(;;)
{
XData d = de->Read();
XLOGI("Read data size is %d",d.size);
}*/
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_xplay_XPlay_InitView(JNIEnv *env, jobject thiz, jobject surface) {
ANativeWindow *win = ANativeWindow_fromSurface(env,surface);
XEGL::Get()->Init(win);
}
#include <string>
#include <android/native_window_jni.h>
#include "FFDemux.h"
#include "XLog.h"
#include "FFDecode.h"
#include "XEGL.h"
class TestObs:public IObserver
{
public:
void Update(XData d)
{
//XLOGI("TestObs Update data size is %d",d.size);
}
};
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_example_xplay_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
//XLOGI("S begin!");
//XSleep(3000);
//XLOGI("S end!");
//return env->NewStringUTF(hello.c_str());
///////////////////////////////////
///测试用代码
TestObs *tobs = new TestObs();
IDemux *de = new FFDemux();
//de->AddObs(tobs);
de->Open("/sdcard/1080.mp4");
IDecode *vdecode = new FFDecode();
vdecode->Open(de->GetVPara());
IDecode *adecode = new FFDecode();
adecode->Open(de->GetAPara());
de->AddObs(vdecode);
de->AddObs(adecode);
//vdecode->Open();
de->Start();
vdecode->Start();
adecode->Start();
//XSleep(3000);
//de->Stop();
/*for(;;)
{
XData d = de->Read();
XLOGI("Read data size is %d",d.size);
}*/
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_xplay_XPlay_InitView(JNIEnv *env, jobject thiz, jobject surface) {
ANativeWindow *win = ANativeWindow_fromSurface(env,surface);
XEGL::Get()->Init(win);
}
然后要在视图文件中创建一个视频视图cpp/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.xplay.XPlay
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Xplay.java中要获取Surface:
package com.example.xplay;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
/**
* Created by Administrator on 2018-03-04.
*/
public class XPlay extends GLSurfaceView implements SurfaceHolder.Callback {
public XPlay(Context context, AttributeSet attrs) {
super( context, attrs );
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
//初始化opengl egl 显示
InitView(holder.getSurface());
}
@Override
public void surfaceChanged(SurfaceHolder var1, int var2, int var3, int var4)
{
}
@Override
public void surfaceDestroyed(SurfaceHolder var1)
{
}
public native void InitView(Object surface);
}
然后在cpp/XEGL.cpp中具体的实现初始化:
#include <android/native_window.h>
#include <EGL/egl.h>
#include "XEGL.h"
#include "XLog.h"
class CXEGL:public XEGL
{
public:
EGLDisplay display = EGL_NO_DISPLAY;
EGLSurface surface = EGL_NO_SURFACE;
EGLContext context = EGL_NO_CONTEXT;
virtual bool Init(void *win)
{
ANativeWindow *nwin = (ANativeWindow *)win;
//初始化EGL
//1 获取EGLDisplay对象显示设备
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if(display == EGL_NO_DISPLAY)
{
XLOGE("eglGetDisplay failed!");
return false;
}
XLOGE("eglGetDisplay success!");
//2 初始化Display
if(EGL_TRUE != eglInitialize(display,0,0))
{
XLOGE("eglInitialize failed!");
return false;
}
XLOGE("eglInitialize success!");
//3 获取配置并创建surface
EGLint configSpec [] = {
EGL_RED_SIZE,8,
EGL_GREEN_SIZE,8,
EGL_BLUE_SIZE,8,
EGL_SURFACE_TYPE,EGL_WINDOW_BIT,
EGL_NONE
};
EGLConfig config = 0;
EGLint numConfigs = 0;
if(EGL_TRUE != eglChooseConfig(display,configSpec,&config,1,&numConfigs))
{
XLOGE("eglChooseConfig failed!");
return false;
}
XLOGE("eglChooseConfig success!");
surface = eglCreateWindowSurface(display,config,nwin,NULL);
//4 创建并打开EGL上下文
const EGLint ctxAttr[] = { EGL_CONTEXT_CLIENT_VERSION ,2, EGL_NONE};
context = eglCreateContext(display,config,EGL_NO_CONTEXT,ctxAttr);
if(context == EGL_NO_CONTEXT)
{
XLOGE("eglCreateContext failed!");
return false;
}
XLOGE("eglCreateContext success!");
if(EGL_TRUE != eglMakeCurrent(display,surface,surface,context))
{
XLOGE("eglMakeCurrent failed!");
return false;
}
XLOGE("eglMakeCurrent success!");
return true;
}
};
XEGL *XEGL::Get()
{
static CXEGL egl;
return &egl;
}
魅族16T表示依然没有问题
关键字词:IEGL
相关文章
-
无相关信息