[Android] NDK 里 ANativeWindow 和 Surface 之间的关系

在MediaCodec里面可以看到 Configure 和 SetSurface 都是接收一个 sp<Surface> 入参的,而回归到 NDK 侧,则可以发现,传递给 AMediaCodec_Configure 和 AMediaCodec_SetSurface 的确是 ANativeWindow 指针,因此 ndk 里面必然进行了某种转换。

cpp 复制代码
EXPORT
media_status_t AMediaCodec_configure(
        AMediaCodec *mData,
        const AMediaFormat* format,
        ANativeWindow* window,
        AMediaCrypto *crypto,
        uint32_t flags) {
    sp<AMessage> nativeFormat;
    AMediaFormat_getFormat(format, &nativeFormat);
    // create our shallow copy, so we aren't victim to any later changes.
    sp<AMessage> dupNativeFormat = nativeFormat->dup();
    ALOGV("configure with format: %s", dupNativeFormat->debugString(0).c_str());
    sp<Surface> surface = NULL;
    if (window != NULL) {
        surface = (Surface*) window;
    }

    status_t err = mData->mCodec->configure(dupNativeFormat, surface,
            crypto ? crypto->mCrypto : NULL, flags);
    if (err != OK) {
        ALOGE("configure: err(%d), failed with format: %s",
              err, dupNativeFormat->debugString(0).c_str());
    }
    return translate_error(err);
}
cpp 复制代码
//prebuilts/vndk/v32/arm64/include/frameworks/native/libs/gui/include/gui/Surface.h


/*
 * An implementation of ANativeWindow that feeds graphics buffers into a
 * BufferQueue.
 *
 * This is typically used by programs that want to render frames through
 * some means (maybe OpenGL, a software renderer, or a hardware decoder)
 * and have the frames they create forwarded to SurfaceFlinger for
 * compositing.  For example, a video decoder could render a frame and call
 * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by
 * Surface.  Surface then forwards the buffers through Binder IPC
 * to the BufferQueue's producer interface, providing the new frame to a
 * consumer such as GLConsumer.
 */
class Surface
    : public ANativeObjectBase<ANativeWindow, Surface, RefBase>

由此可见, Surface 类是 ANativeWindow 的子类。

相关推荐
alexhilton17 小时前
务实的模块化:连接模块(wiring modules)的妙用
android·kotlin·android jetpack
ji_shuke17 小时前
opencv-mobile 和 ncnn-android 环境配置
android·前端·javascript·人工智能·opencv
sunnyday042619 小时前
Spring Boot 项目中使用 Dynamic Datasource 实现多数据源管理
android·spring boot·后端
幽络源小助理21 小时前
下载安装AndroidStudio配置Gradle运行第一个kotlin程序
android·开发语言·kotlin
inBuilder低代码平台21 小时前
浅谈安卓Webview从初级到高级应用
android·java·webview
豌豆学姐21 小时前
Sora2 短剧视频创作中如何保持人物一致性?角色创建接口教程
android·java·aigc·php·音视频·uniapp
白熊小北极21 小时前
Android Jetpack Compose折叠屏感知与适配
android
HelloBan21 小时前
setHintTextColor不生效
android
洞窝技术1 天前
从0到30+:智能家居配网协议融合的实战与思考
android
QING6181 天前
SupervisorJob子协程异常处理机制 —— 新手指南
android·kotlin·android jetpack