[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 的子类。

相关推荐
huwuhang9 小时前
Winkawaks街机游戏模拟器整合ROM版 1.65 汉化修正完整版 2026.03.30
android·游戏·游戏机
yitian_hm9 小时前
MySQL主从复制与读写分离实战指南
android·mysql·adb
NEGl DRYN9 小时前
Plugin ‘mysql_native_password‘ is not loaded`
android·数据库·mysql
Derrick__110 小时前
Android混淆和加密技术
android·jvm·python
sunwenjian88610 小时前
MySQL加减间隔时间函数DATE_ADD和DATE_SUB的详解
android·数据库·mysql
ictI CABL11 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
放学以后Nicetry12 小时前
Android SELinux 指南:从基本概念到实战修复
android
CCIE-Yasuo13 小时前
《永恒战士2-无双战神》无限金币版(提供apk下载)安卓Android逆向记录学习-Deepseek-AI辅助
android·java·学习·游戏
jzlhll12314 小时前
kotlin flow去重distinctUntilChanged vs distinctUntilChangedBy
android·开发语言·kotlin
渡我白衣14 小时前
【MySQL基础】(3):MySQL库与表的操作
android·数据库·人工智能·深度学习·神经网络·mysql·adb