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

相关推荐
好的佩奇2 小时前
Dart 之可迭代对象与生成器
android·flutter·dart
_祝你今天愉快3 小时前
Paint Cavans高级绘制:[三] Canvas高阶使用技巧-变换,状态保存,离屏缓冲,粒子特效
android
小乖写代码3 小时前
提交注册验证 · WhatsApp /v2/register
android·网络·账号注册
二流小码农7 小时前
鸿蒙开发:router组件升级,彻底告别手动配置
android·ios·harmonyos
Digitally8 小时前
如何在安卓设备上使用 4 种简单方法将应用移动到 SD 卡
android
booboosui9 小时前
录一遍就会:8 种步骤、6 类失败,卡住时只修那一步
android
阿杰m10 小时前
Android源码第十五章:Camera2 与 CameraService——一帧图像怎样回到应用
android
恋猫de小郭15 小时前
Flutter Analyzer 系列坑推进,必看,AI 时代这玩意最吃你内存
android·前端·flutter
LinXunFeng1 天前
拿竞品 App 一扫,技术栈全暴露了
android·flutter·开源
Gracker1 天前
AIW:从系统机制到性能排查,一本可以对照源码的 Android 电子书
android