SurfaceView简介
SurfaceView对比View的区别
安卓的普通VIew,都依赖于当前Activity的Window的surface,这个surface用于承载view树从底到顶绘制出来的所有内容,因此任何一个view需要更新时,都需要把所有view中底到顶进行更新,即使使用脏区域,也依然要把其他view中相应的像素进行合成操作,因此不适合频繁的更新绘制,而且更新过程只能依靠UI线程。
而SurfaceView却有自己的Surface,因此可以单独更新其内容而不触发整个view树的更新,在子线程刷新不会阻塞主线程,适用于界面频繁更新、对帧率要求较高的情况,因此十分适合于视频渲染。而很多视频渲染库如FFMpeg,或者有时候一些开源OpenGL ES代码是在Native层编写的,如果要在折腾到Java层进行显示是非常麻烦的,所以SurfaceView在Native层面的更新在安卓图像处理输出中是一个必须懂的技能点。
SurfaceView的ANativeWindow的获取和使用
Surface对象不能直接在jni native层使用,因此需要通过Android NDK工具获取它的本地对象ANativeWindow,
这里借用一下大佬的一张图来表达SurfaceView中ANativeWindow的调用过程描述和流程图:
● java层将Surface传递给native层
● 获取ANativeWindow对象
● 将显示数据写到ANativeWindow的buffer中,注意需要将显示的数据格式转换成ANativeWindow设置的数据格式
● 释放ANativeWindow
最简测试Demo
gradle配置:
主要是打开NativeBuild、指定创建ABI编译目标、以及cmake配置文件位置
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdkVersion 34
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.learnopengl"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
ndk {
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
}
}
//……
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
jni.srcDirs = []
}
}
ndkVersion '20.1.5948944'
kotlinOptions {
jvmTarget = '1.8'
}
}
根CMakeLists配置:
这个只是我个人的配置,大家可以根据实际情况修改
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
add_compile_options(
-fno-omit-frame-pointer
-fexceptions
-Wall
)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -mfloat-abi=soft -DANDROID")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -mfloat-abi=soft -DANDROID")
# 生成中间文件,便于debug
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -save-temps=obj")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -save-temps=obj")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
ADD_SUBDIRECTORY(src/main/cpp/opengl_decoder)
子工程opengl_decoder的CMake配置
声明工程文件位置、工程名,和要引入的库,编译后的名字
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
#project(zbar LANGUAGES C VERSION 2.0.2)
project(opengl_decoder)
#SET(zbar_sdk_dir ${CMAKE_SOURCE_DIR}/src/main/cpp/opengl_decoder)
message(AUTHOR_WARNING ${CMAKE_CURRENT_SOURCE_DIR})
message(AUTHOR_WARNING ${opengl_decoder})
add_definitions("-DDYNAMIC_ES3")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
#INCLUDE_DIRECTORIES("/lib")
add_library( # Sets the name of the library.
opengl_decoder
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
OpenGLNativeRenderJNIBridgeDemo.h
OpenGLNativeRenderJNIBridgeDemo.cpp
)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library(log-lib log)
find_library(android-lib android)
find_library(EGL-lib EGL)
#find_library(GLESv2-lib GLESv2)
find_library(GLESv3-lib GLESv3)
find_library(OpenSLES-lib OpenSLES)
find_library(dl-lib dl)
find_library(z-lib z)
target_link_libraries(
opengl_decoder
jnigraphics
${log-lib}
${android-lib}
${EGL-lib}
${GLESv3-lib}
${OpenSLES-lib}
${dl-lib}
${z-lib}
#数学库:
m
)
${PROJECT_SOURCE_DIR}/lib/${ANDROID_ABI}/libiconv.so)
message(AUTHOR_WARNING ${PROJECT_SOURCE_DIR})
实际逻辑代码:
1、 先编写一个SurfaceView子类,然后编写一个子线程TestThread,意图循环输出随机的颜色清屏信号,通过获取SurfaceView的holder内部Surface、以及清屏颜色传入到事先编写的native方法JniBridge.drawToSurface中实现。
package com.cjztest.glOffscreenProcess.demo1
import android.content.Context
import android.graphics.Color
import android.graphics.PixelFormat
import android.util.AttributeSet
import android.view.SurfaceHolder
import android.view.SurfaceView
import com.opengldecoder.jnibridge.JniBridge
import kotlin.random.Random
class NativeModifySurfaceView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : SurfaceView(context, attrs), SurfaceHolder.Callback {
private lateinit var mSurfaceHolder: SurfaceHolder
inner class TestThread : Thread() {
override fun run() {
while(this@NativeModifySurfaceView.isAttachedToWindow) {
JniBridge.drawToSurface(holder.surface
, (0xFF000000.toInt()
or (Random.nextFloat() * 255f).toInt()
or ((Random.nextFloat() * 255f).toInt() shl 8)
or ((Random.nextFloat() * 255f).toInt() shl 16)))
sleep(16)
}
}
}
init {
mSurfaceHolder = holder
mSurfaceHolder.addCallback(this)
mSurfaceHolder.setFormat(PixelFormat.RGBA_8888)
isFocusable = true
setFocusableInTouchMode(true)
}
private var mTestThread: TestThread ?= null
override fun surfaceCreated(holder: SurfaceHolder) {
if (mTestThread == null) {
mTestThread = TestThread()
}
mTestThread?.start()
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
}
}
其中要注意的是,为了确保其中Surface的像素格式为RGBA8888,方便进行颜色填充实验,我在类初始化时通过SurfaceHolder将其颜色设置为RGBA8888了:
mSurfaceHolder.setFormat(PixelFormat.RGBA_8888)
2、 创建JniBridge类,编写JNI方法签名,可以传入surface对象,交由JNIEnv进行处理:
package com.opengldecoder.jnibridge;
import android.graphics.Bitmap;
import android.view.Surface;
public class JniBridge {
static {
System.loadLibrary("opengl_decoder");
}
public static native void drawToSurface(Surface surface, int color);
}
3、 编写JNI方法,获取Surface的ANativeWindow,然后对其进行颜色填充,步骤和使用SurfaceView是类似的。过程如下:
先通过ANativeWindow_fromSurface获取传入的Surface的ANativeWindow对象,再通过ANativeWindow_lock锁定Surface并获取它的数据buffer指针,然后把传入的color值循环便利写入该对象的,最后调用ANativeWindow_release解锁即可看到Surface被刷成指定颜色了。
//cpp/opengl_decoder/OpenGLNativeRenderJNIBridgeDemo.cpp
JNIEXPORT void JNICALL
Java_com_opengldecoder_jnibridge_JniBridge_drawToSurface(JNIEnv *env, jobject activity,
jobject surface, jint color) {
ANativeWindow_Buffer nwBuffer;
LOGI("ANativeWindow_fromSurface ");
ANativeWindow *mANativeWindow = ANativeWindow_fromSurface(env, surface);
if (mANativeWindow == NULL) {
LOGE("ANativeWindow_fromSurface error");
return;
}
LOGI("ANativeWindow_lock ");
if (0 != ANativeWindow_lock(mANativeWindow, &nwBuffer, 0)) {
LOGE("ANativeWindow_lock error");
return;
}
LOGI("ANativeWindow_lock nwBuffer->format ");
if (nwBuffer.format == WINDOW_FORMAT_RGBA_8888) {
LOGI("nwBuffer->format == WINDOW_FORMAT_RGBA_8888 ");
for (int i = 0; i < nwBuffer.height * nwBuffer.width; i++) {
*((int*)nwBuffer.bits + i) = color;
}
}
LOGI("ANativeWindow_unlockAndPost ");
if (0 != ANativeWindow_unlockAndPost(mANativeWindow)) {
LOGE("ANativeWindow_unlockAndPost error");
return;
}
ANativeWindow_release(mANativeWindow);
LOGI("ANativeWindow_release ");
}
4、 让SurfaceHolder的在创建时生成测试线程对象,测试线程将循环合成不同的颜色然后传入刚才到刚才的native函数逻辑中,实现native层面对surface进行内容填充实例的目的:
override fun surfaceCreated(holder: SurfaceHolder) {
if (mTestThread == null) {
mTestThread = TestThread()
}
mTestThread?.start()
}
inner class TestThread : Thread() {
override fun run() {
while(this@NativeModifySurfaceView.isAttachedToWindow) {
JniBridge.drawToSurface(holder.surface
, (0xFF000000.toInt()
or (Random.nextFloat() * 255f).toInt()
or ((Random.nextFloat() * 255f).toInt() shl 8)
or ((Random.nextFloat() * 255f).toInt() shl 16)))
sleep(16)
}
}
}
效果:
截两次不同颜色的填充结果:
结尾:
本文内容主要是展示了如何搭建一个native层面填充Surface内容的环境,在实际场景中可以用于FFMpeg解码后内容、OpenGL ES渲染后拷贝并显示到Surface的。
引用:
Android的Surface、View、SurfaceView、Window概念整理 | superxlcr's notebook
Android基础--利用ANativeWindow显示视频-腾讯云开发者社区-腾讯云