Android使用ANativeWindow更新surfaceView内容最简Demo

SurfaceView简介

SurfaceView对比View的区别

        安卓的普通VIew,依赖当前ActivityWindowsurface这个surface用于承载view绘制出来所有内容因此任何一个view需要更新需要所有view进行更新即使使用区域依然其他view相应像素进行合成操作因此不适合频繁更新绘制而且更新过程只能UI线程。

        而SurfaceView自己Surface因此可以单独更新内容触发整个view更新在子线程刷新不会阻塞主线程,适用于界面频繁更新、对帧率要求较高的情况因此十分适合于视频渲染。而很多视频渲染库如FFMpeg,或者有时候一些开源OpenGL ES代码Native编写,如果要在折腾到Java层进行显示是非常麻烦的,所以SurfaceViewNative层面更新安卓图像处理输出一个必须技能

SurfaceView的ANativeWindow的获取和使用

        Surface对象不能直接jni native使用因此需要通过Android NDK工具获取本地对象ANativeWindow

        这里借用一下大佬一张图表达SurfaceViewANativeWindow调用过程描述和流程图:

 

● 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方法,获取SurfaceANativeWindow然后对其进行颜色填充步骤使用SurfaceView类似的。过程如下:

通过ANativeWindow_fromSurface获取传入SurfaceANativeWindow对象,再通过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显示视频-腾讯云开发者社区-腾讯云

  • 20
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值