Skip to content

Commit

Permalink
y优化渲染
Browse files Browse the repository at this point in the history
  • Loading branch information
MJLblabla committed Sep 4, 2022
1 parent 0f7c5ee commit 97b3bab
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 78 deletions.
6 changes: 6 additions & 0 deletions SMBeautyEngine_andriod/pixelfree_android_demo/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class MainActivity : AppCompatActivity() {

//数据处理回调
override fun onProcessFrame(frame: VideoFrame): VideoFrame {

if(!mPixelFree.isCreate()){
OpenGLTools.switchContext()
mPixelFree.create()
Expand All @@ -40,15 +39,7 @@ class MainActivity : AppCompatActivity() {
)
}else{
mPixelFree.processWithBuffer(frame.toPFIamgeInput())
val code =GLES30.glGetError()
Log.d("mjl","GLES30.glGetError()" + code)
val byteBuffer = ByteBuffer.wrap(ByteArray(frame.data.size))
GLES30.glReadPixels(0, 0, frame.width, frame.height,
GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, byteBuffer);
val stitchBmp = Bitmap.createBitmap(frame.width, frame.height, Bitmap.Config.ARGB_8888)
stitchBmp.copyPixelsFromBuffer(byteBuffer)
}

return super.onProcessFrame(frame)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class CameraXTrack internal constructor(
cameraProviderFuture.addListener(Runnable {
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
val cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA

try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ abstract class IVideoTrack internal constructor() : Track<VideoFrame> {

abstract class IAudioTrack internal constructor() : Track<AudioFrame> {


override var frameCall: FrameCall<AudioFrame>? = null

var mAudioRender: AudioRender? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,56 +117,62 @@ void VideoGLRender::RenderVideoFrame(NativeImage *image) {
m_RenderImage = nullptr;
}
}
switch (format) {
case IMAGE_FORMAT_I420:
if (m_RenderImage == nullptr) {
m_RenderImage = new NativeImage();
m_RenderImage->pLineSize[0] = dataLen;
m_RenderImage->ppPlane[0] = static_cast<uint8_t *>(malloc(dataLen));
}
memcpy(m_RenderImage->ppPlane[0], image->ppPlane[0], dataLen);
m_RenderImage->ppPlane[1] = m_RenderImage->ppPlane[0] + width * height;
m_RenderImage->ppPlane[2] = m_RenderImage->ppPlane[1] + width * height / 4;
break;
case IMAGE_FORMAT_NV12:
case IMAGE_FORMAT_NV21:
if (m_RenderImage == nullptr) {
m_RenderImage = new NativeImage();
m_RenderImage->pLineSize[0] = dataLen;
m_RenderImage->ppPlane[0] = static_cast<uint8_t *>(malloc(dataLen));
}
memcpy(m_RenderImage->ppPlane[0], image->ppPlane[0], dataLen);
m_RenderImage->ppPlane[1] = m_RenderImage->ppPlane[0] + width * height;
break;
default:
//rgba
int lenNew = width * height * pixel_stride;
if (m_RenderImage == nullptr) {
m_RenderImage = new NativeImage();
m_RenderImage->pLineSize[0] = lenNew;
m_RenderImage->ppPlane[0] = static_cast<uint8_t *>(malloc(lenNew));
}

if (m_RenderImage->pLineSize[0] != lenNew) {
m_RenderImage->pLineSize[0] = lenNew;
free(m_RenderImage->ppPlane[0]);
m_RenderImage->ppPlane[0] = static_cast<uint8_t *>(malloc(lenNew));
}
if (row_padding > 0) {
int row = height;
int line = width * pixel_stride;
for (int i = 0; i < row; i++) {
int start = line * i;
int start2 = (width * pixel_stride + row_padding) * i;
memcpy(m_RenderImage->ppPlane[0] + start, image->ppPlane[0] + start2, line);

if (image->textureID <= 0) {
switch (format) {
case IMAGE_FORMAT_I420:
if (m_RenderImage == nullptr) {
m_RenderImage = new NativeImage();
m_RenderImage->pLineSize[0] = dataLen;
m_RenderImage->ppPlane[0] = static_cast<uint8_t *>(malloc(dataLen));
}
} else {
memcpy(m_RenderImage->ppPlane[0], image->ppPlane[0], dataLen);
}
break;
}
m_RenderImage->ppPlane[1] = m_RenderImage->ppPlane[0] + width * height;
m_RenderImage->ppPlane[2] = m_RenderImage->ppPlane[1] + width * height / 4;
break;
case IMAGE_FORMAT_NV12:
case IMAGE_FORMAT_NV21:
if (m_RenderImage == nullptr) {
m_RenderImage = new NativeImage();
m_RenderImage->pLineSize[0] = dataLen;
m_RenderImage->ppPlane[0] = static_cast<uint8_t *>(malloc(dataLen));
}
memcpy(m_RenderImage->ppPlane[0], image->ppPlane[0], dataLen);
m_RenderImage->ppPlane[1] = m_RenderImage->ppPlane[0] + width * height;
break;
default:
//rgba
int lenNew = width * height * pixel_stride;
if (m_RenderImage == nullptr) {
m_RenderImage = new NativeImage();
m_RenderImage->pLineSize[0] = lenNew;
m_RenderImage->ppPlane[0] = static_cast<uint8_t *>(malloc(lenNew));
}

m_RenderImage->textureID = image->textureID;
if (m_RenderImage->pLineSize[0] != lenNew) {
m_RenderImage->pLineSize[0] = lenNew;
free(m_RenderImage->ppPlane[0]);
m_RenderImage->ppPlane[0] = static_cast<uint8_t *>(malloc(lenNew));
}
if (row_padding > 0) {
int row = height;
int line = width * pixel_stride;
for (int i = 0; i < row; i++) {
int start = line * i;
int start2 = (width * pixel_stride + row_padding) * i;
memcpy(m_RenderImage->ppPlane[0] + start, image->ppPlane[0] + start2, line);
}
} else {
memcpy(m_RenderImage->ppPlane[0], image->ppPlane[0], dataLen);
}
break;
}
} else {
if (m_RenderImage == nullptr) {
m_RenderImage = new NativeImage();
}
m_RenderImage->textureID = image->textureID;
}
m_RenderImage->rotationDegrees = rotation_degrees;
m_RenderImage->width = width;
m_RenderImage->height = height;
Expand Down Expand Up @@ -274,7 +280,7 @@ void VideoGLRender::OnSurfaceCreated() {

glGenTextures(TEXTURE_NUM, m_TextureIds);
for (int i = 0; i < TEXTURE_NUM; ++i) {
LOGCATE("VideoGLRender::glGenTextures %d",m_TextureIds[i]);
LOGCATE("VideoGLRender::glGenTextures %d", m_TextureIds[i]);
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, m_TextureIds[i]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Expand Down Expand Up @@ -336,15 +342,16 @@ void VideoGLRender::OnDrawFrame() {

m_FrameIndex++;

if(m_RenderImage->textureID!=-1000){
m_TextureIds[0]=m_RenderImage->textureID;
LOGCATE("VideoGLRender::m_RenderImage->textureID!=-1000 %d]",m_RenderImage->textureID);
} else{
if (m_RenderImage->textureID > 0) {
m_TextureIds[0] = m_RenderImage->textureID;
LOGCATE("VideoGLRender::m_RenderImage->textureID!=-1000 %d]", m_RenderImage->textureID);
} else {
switch (m_RenderImage->format) {
case IMAGE_FORMAT_RGBA:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_TextureIds[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_RenderImage->width, m_RenderImage->height, 0,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_RenderImage->width, m_RenderImage->height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE, m_RenderImage->ppPlane[0]);
glBindTexture(GL_TEXTURE_2D, GL_NONE);
Expand Down Expand Up @@ -395,18 +402,13 @@ void VideoGLRender::OnDrawFrame() {
default:
break;
}

}

// Use the program object
glUseProgram(m_ProgramObj);

glBindVertexArray(m_VaoId);

glm::mat4 Model = glm::mat4(1.0f);
auto radiansZ = -static_cast<float>(MATH_PI / 180.0f * m_RenderImage->rotationDegrees);
Model = glm::rotate(Model, radiansZ, glm::vec3(0.0f, 0.0f, 1.0f));

GLUtils::setMat4(m_ProgramObj, "u_MVPMatrix", m_MVPMatrix * Model);

for (int i = 0; i < TEXTURE_NUM; ++i) {
Expand All @@ -417,9 +419,6 @@ void VideoGLRender::OnDrawFrame() {
GLUtils::setInt(m_ProgramObj, samplerName, i);
}

//float time = static_cast<float>(fmod(m_FrameIndex, 60) / 50);
//GLUtils::setFloat(m_ProgramObj, "u_Time", time);

float offset = (sin(m_FrameIndex * MATH_PI / 40) + 1.0) / 2.0f;
GLUtils::setFloat(m_ProgramObj, "u_Offset", offset);
GLUtils::setVec2(m_ProgramObj, "u_TexSize", vec2(m_RenderImage->width, m_RenderImage->height));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ class OpenGLRender : GLSurfaceView.Renderer, VideoRender {
}

var glCreateCall: (p1: EGLConfig) -> Unit = {

}

var glReleaseCall: () -> Unit = {

}

override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?) {
Expand All @@ -38,7 +33,6 @@ class OpenGLRender : GLSurfaceView.Renderer, VideoRender {
}

fun release() {
glReleaseCall.invoke()
native_release(renderHandler)
}

Expand Down Expand Up @@ -79,7 +73,11 @@ class OpenGLRender : GLSurfaceView.Renderer, VideoRender {
frame.width,
frame.height,
frame.format.fmt,
frame.data,
if (frame.textureID <= 0) {
frame.data
} else {
ByteArray(0)
},
frame.rotationDegrees,
frame.pixelStride,
frame.rowPadding
Expand Down

0 comments on commit 97b3bab

Please sign in to comment.