Browse Source

修复CatchBuffer宽高

jeofferyli 4 years ago
parent
commit
1d959b95b2

+ 1 - 0
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimPlayer.kt

@@ -69,6 +69,7 @@ class AnimPlayer(val animView: IAnimView) {
         isSurfaceAvailable = true
         isSurfaceAvailable = true
         startRunnable?.run()
         startRunnable?.run()
         startRunnable = null
         startRunnable = null
+        decoder?.onSurfaceSizeChanged(width, height)
     }
     }
 
 
 
 

+ 14 - 3
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/CacheBuffer.kt

@@ -1,14 +1,18 @@
 package com.tencent.qgame.animplayer
 package com.tencent.qgame.animplayer
 
 
+import android.graphics.Bitmap
 import android.opengl.GLES11Ext
 import android.opengl.GLES11Ext
 import android.opengl.GLES20
 import android.opengl.GLES20
+import android.os.Environment
 import android.util.Log
 import android.util.Log
 import com.tencent.qgame.animplayer.util.*
 import com.tencent.qgame.animplayer.util.*
 import android.util.DisplayMetrics
 import android.util.DisplayMetrics
 
 
 import android.view.WindowManager
 import android.view.WindowManager
-
-
+import java.io.BufferedOutputStream
+import java.io.FileOutputStream
+import java.io.IOException
+import java.nio.ByteBuffer
 
 
 
 
 /**
 /**
@@ -32,7 +36,9 @@ class CacheBuffer {
     private var aTextureAlphaLocation: Int = 0
     private var aTextureAlphaLocation: Int = 0
     private var aTextureRgbLocation: Int = 0
     private var aTextureRgbLocation: Int = 0
 
 
-    
+    private var width: Int = 0
+    private var height: Int = 0
+
     // 输入纹理ID
     // 输入纹理ID
     private var textureId = -1
     private var textureId = -1
     // FBO纹理ID
     // FBO纹理ID
@@ -100,6 +106,8 @@ class CacheBuffer {
      */
      */
     fun genCatch(width: Int, height: Int) {
     fun genCatch(width: Int, height: Int) {
         ALog.d(TAG, "genCatch: $width x $height")
         ALog.d(TAG, "genCatch: $width x $height")
+        this.width = width
+        this.height = height
         GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboFrameBuffer)
         GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboFrameBuffer)
         GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, fboTextureId)
         GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, fboTextureId)
         GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, fboTextureId, 0)
         GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, fboTextureId, 0)
@@ -135,6 +143,9 @@ class CacheBuffer {
         GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
         GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
         checkGLError("glDrawArrays drawFBO")
         checkGLError("glDrawArrays drawFBO")
 
 
+        // todo : 测试用,记得删
+//        saveRgb2Bitmap(intArrayOf(230), width, height, "fbo")
+
         GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0)
         GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0)
         checkGLError("glBindTexture 0 drawFBO")
         checkGLError("glBindTexture 0 drawFBO")
         GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0)
         GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0)

+ 13 - 4
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/Render.kt

@@ -43,6 +43,7 @@ class Render(surfaceTexture: SurfaceTexture): IRenderListener {
 
 
     private var catchTextureId = -1
     private var catchTextureId = -1
 
 
+    // 是否使用fbo缓冲
     private var useFbo = true
     private var useFbo = true
 
 
     private val catchBuf by lazy { CacheBuffer() }
     private val catchBuf by lazy { CacheBuffer() }
@@ -61,7 +62,7 @@ class Render(surfaceTexture: SurfaceTexture): IRenderListener {
 
 
     private fun setVertexBuf(config: AnimConfig) {
     private fun setVertexBuf(config: AnimConfig) {
         val fArr = VertexUtil.create(config.width, config.height, PointRect(0, 0, config.width, config.height), vertexArray.array)
         val fArr = VertexUtil.create(config.width, config.height, PointRect(0, 0, config.width, config.height), vertexArray.array)
-        ALog.d(CacheBuffer.TAG, "setVertexArr: ${fArr.toList()}")
+        ALog.d(TAG, "setVertexArr: ${fArr.toList()}")
 
 
         if (useFbo) {
         if (useFbo) {
             catchBuf.setVertexArr(fArr)
             catchBuf.setVertexArr(fArr)
@@ -72,12 +73,11 @@ class Render(surfaceTexture: SurfaceTexture): IRenderListener {
     private fun setTexCoords(config: AnimConfig) {
     private fun setTexCoords(config: AnimConfig) {
         val rgb = TexCoordsUtil.create(config.videoWidth, config.videoHeight, config.rgbPointRect, rgbArray.array)
         val rgb = TexCoordsUtil.create(config.videoWidth, config.videoHeight, config.rgbPointRect, rgbArray.array)
         val alpha = TexCoordsUtil.create(config.videoWidth, config.videoHeight, config.alphaPointRect, alphaArray.array)
         val alpha = TexCoordsUtil.create(config.videoWidth, config.videoHeight, config.alphaPointRect, alphaArray.array)
-        ALog.d(CacheBuffer.TAG, "setTexArr: texRgbArray: ${rgb.toList()}")
-        ALog.d(CacheBuffer.TAG, "setTexArr: texAlphaArray: ${alpha.toList()}")
+        ALog.d(TAG, "setTexArr: texRgbArray: ${rgb.toList()}")
+        ALog.d(TAG, "setTexArr: texAlphaArray: ${alpha.toList()}")
 
 
         if (useFbo) {
         if (useFbo) {
             catchBuf.setTexArr(rgb, alpha)
             catchBuf.setTexArr(rgb, alpha)
-            catchBuf.genCatch(config.width, config.height)
             rgbArray.setArray(mTextureCoors)
             rgbArray.setArray(mTextureCoors)
             alphaArray.setArray(mTextureCoors)
             alphaArray.setArray(mTextureCoors)
         } else {
         } else {
@@ -118,6 +118,9 @@ class Render(surfaceTexture: SurfaceTexture): IRenderListener {
             }
             }
         }
         }
 
 
+        // todo 测试用,记得删
+        updateSave2BitmapIndex()
+
         if (useFbo) {
         if (useFbo) {
             catchBuf.draw()
             catchBuf.draw()
         }
         }
@@ -134,6 +137,9 @@ class Render(surfaceTexture: SurfaceTexture): IRenderListener {
     override fun destroyRender() {
     override fun destroyRender() {
         releaseTexture()
         releaseTexture()
         eglUtil.release()
         eglUtil.release()
+
+        // todo 测试用,记得删
+        resetSave2Bitmap()
     }
     }
 
 
     override fun releaseTexture() {
     override fun releaseTexture() {
@@ -191,6 +197,9 @@ class Render(surfaceTexture: SurfaceTexture): IRenderListener {
         // draw
         // draw
         GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
         GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
 
 
+        // todo : 测试用,记得删
+//        saveRgb2Bitmap(intArrayOf(230), surfaceWidth, surfaceHeight, "def")
+
         if (useFbo) {
         if (useFbo) {
             GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, GLES20.GL_NONE)
             GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, GLES20.GL_NONE)
         } else {
         } else {

+ 61 - 0
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/util/GlTools.kt

@@ -1,6 +1,14 @@
 package com.tencent.qgame.animplayer.util
 package com.tencent.qgame.animplayer.util
 
 
+import android.graphics.Bitmap
 import android.opengl.GLES20
 import android.opengl.GLES20
+import android.os.Environment
+import android.util.Log
+import com.tencent.qgame.animplayer.CacheBuffer
+import java.io.BufferedOutputStream
+import java.io.FileOutputStream
+import java.io.IOException
+import java.nio.ByteBuffer
 
 
 /**
 /**
  * Date: 2022/1/12
  * Date: 2022/1/12
@@ -17,4 +25,57 @@ fun checkGLError(extMsg: String? = "none") {
     if (GLES20.glGetError().also { error = it } != GLES20.GL_NO_ERROR) {
     if (GLES20.glGetError().also { error = it } != GLES20.GL_NO_ERROR) {
         ALog.e("CheckGLError", "extMsg: $extMsg, >> ERROR: $error <<")
         ALog.e("CheckGLError", "extMsg: $extMsg, >> ERROR: $error <<")
     }
     }
+}
+
+private var rgbaBuf: ByteBuffer? = null
+private var index = 0
+
+@Synchronized
+fun saveRgb2Bitmap(frameIndex: IntArray, width: Int, height: Int, msg: String)
+{
+    if (frameIndex.contains(index)) {
+        if (null == rgbaBuf) {
+            rgbaBuf = ByteBuffer.allocateDirect(width * height * 4)
+        }
+        rgbaBuf?.let {
+            it.position(0)
+            val start = System.nanoTime()
+            GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, rgbaBuf)
+            val end = System.nanoTime()
+            Log.d(CacheBuffer.TAG, "glReadPixels: " + (end - start))
+
+            Log.d(CacheBuffer.TAG, "saveRgb2Bitmap: msg: $msg,  index = $index")
+            var bos: BufferedOutputStream? = null
+            try {
+                bos = BufferedOutputStream(FileOutputStream(Environment.getExternalStorageDirectory().absolutePath + "/AAVapTest/" + "$index" + "_$msg.png"))
+                val bmp = Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888)
+                bmp.copyPixelsFromBuffer(it)
+                bmp.compress(Bitmap.CompressFormat.PNG, 90, bos)
+                bmp.recycle()
+            } catch (e: IOException) {
+                e.printStackTrace()
+                Log.e(CacheBuffer.TAG, "saveRgb2Bitmap: $e")
+            } finally {
+                if (bos != null) {
+                    try {
+                        bos.close()
+                    } catch (e: IOException) {
+                        e.printStackTrace()
+                        Log.e(CacheBuffer.TAG, "saveRgb2Bitmap: $e")
+                    }
+                }
+            }
+        }
+    }
+
+}
+
+fun updateSave2BitmapIndex() {
+    index++
+}
+
+fun resetSave2Bitmap() {
+    index = -1
+    rgbaBuf?.clear()
+    rgbaBuf = null
 }
 }

+ 12 - 10
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/util/TexCoordsUtil.kt

@@ -82,7 +82,7 @@ object TexCoordsUtil {
     }
     }
 
 
     /**
     /**
-     * 顺时针180度
+     * 上下翻转
      */
      */
     fun rotateE(array: FloatArray): FloatArray {
     fun rotateE(array: FloatArray): FloatArray {
         val tx = array[0]
         val tx = array[0]
@@ -92,20 +92,22 @@ object TexCoordsUtil {
         array[0] = array[2]
         array[0] = array[2]
         array[1] = array[3]
         array[1] = array[3]
 
 
-        // 0->1
-        array[2] = tx
-        array[3] = ty
-
-        val tx2 = array[4]
-        val ty2 = array[5]
+        // 暂存 2->1
+        array[2] = array[4]
+        array[3] = array[5]
 
 
         // 3->2
         // 3->2
         array[4] = array[6]
         array[4] = array[6]
         array[5] = array[7]
         array[5] = array[7]
 
 
-        // 2->3
-        array[6] = tx2
-        array[7] = ty2
+        // 2->3 (2在1中)
+        array[6] = array[2]
+        array[7] = array[3]
+
+        // 0->1
+        array[2] = tx
+        array[3] = ty
+
 
 
 
 
         return array
         return array

+ 17 - 2
Android/PlayerProj/app/src/main/java/com/tencent/qgame/playerproj/player/AnimVapxDemoActivity.kt

@@ -58,6 +58,8 @@ class AnimVapxDemoActivity : Activity(), IAnimListener {
 
 
     private var head1Img = true
     private var head1Img = true
 
 
+    private var inited = false
+
     // 视频信息
     // 视频信息
     data class VideoInfo(val fileName: String, val md5: String)
     data class VideoInfo(val fileName: String, val md5: String)
 
 
@@ -86,6 +88,9 @@ class AnimVapxDemoActivity : Activity(), IAnimListener {
         animView = playerView
         animView = playerView
         // 居中(根据父布局按比例居中并裁剪)
         // 居中(根据父布局按比例居中并裁剪)
         animView.setScaleType(ScaleType.CENTER_CROP)
         animView.setScaleType(ScaleType.CENTER_CROP)
+
+        inited = true
+
         /**
         /**
          * 注册资源获取类
          * 注册资源获取类
          */
          */
@@ -222,8 +227,18 @@ class AnimVapxDemoActivity : Activity(), IAnimListener {
 
 
     override fun onPause() {
     override fun onPause() {
         super.onPause()
         super.onPause()
-        // 页面切换是停止播放
-        animView.stopPlay()
+        if (inited) {
+            // 页面切换是停止播放
+            animView.pausePlay()
+        }
+    }
+
+    override fun onResume() {
+        super.onResume()
+        if (inited) {
+            // 页面切换恢复播放
+            animView.resumePlay()
+        }
     }
     }