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

相关推荐
fengci.6 分钟前
Polar春季个人挑战赛WEB简单部分
android·前端
不会写DN19 分钟前
如何使用PHP创建图像验证码
android·开发语言·php
小驰行动派1 小时前
Android Studio中,使用完全免费的Ai来写APP
android·ide·android studio
笔夏2 小时前
【安卓学习之socket】socket.io-client
android·学习
用户94261184462912 小时前
Android开发实战:ListView与RecyclerView使用详解
android
梦里花开知多少2 小时前
深入解析Launcher3 中的 CellLayout
android·面试
WangJunXiang62 小时前
MySQL高可用详细解析
android·数据库·mysql
fengci.2 小时前
polar2026春季个人挑战赛(中等部分)
android
黑马源码库miui520862 小时前
JAVA国际版同城上门服务上门送水桶装水配送源码同城上门配送系统源码支持Android+IOS+H5
android·java·ios
黄林晴3 小时前
无奈妥协!六年账号被迫改名,Android 技术圈顺势升级 !
android·运营