【Android】GraphicBuffer和ANativeWindowBuffer

GraphicBuffer是surface的内存数据载体,

但是,在Surface::lock方法获取内存信息时,

cpp 复制代码
//frameworks/native/libs/gui/Surface.cpp
status_t Surface::lock(
        ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
{
    ......
    ANativeWindowBuffer* out;
    int fenceFd = -1;
    //拿到一个缓冲区
    status_t err = dequeueBuffer(&out, &fenceFd);
    if (err == NO_ERROR) {
        sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
        ......
        void* vaddr;
        status_t res = backBuffer->lockAsync(
                GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
                newDirtyRegion.bounds(), &vaddr, fenceFd);
        if (res != 0) {
            err = INVALID_OPERATION;
        } else {
            mLockedBuffer = backBuffer;
            outBuffer->width  = backBuffer->width;
            outBuffer->height = backBuffer->height;
            outBuffer->stride = backBuffer->stride;
            outBuffer->format = backBuffer->format;
            outBuffer->bits   = vaddr;
        }
    }
    return err;
}

看上去dequeueBuffer调用把数据保存到了ANativeWindowBuffer中,再通过GraphicBuffer的getSelf处理进行了转换,

cpp 复制代码
    ANativeWindowBuffer* out;
    int fenceFd = -1;
    //拿到一个缓冲区
    status_t err = dequeueBuffer(&out, &fenceFd);
    if (err == NO_ERROR) {
        sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));

GraphicBuffer 和 ANativeWindowBuffer是什么关系呢,稍微看一下定义,就可以发现,可以把他们理解为一个东西,

frameworks/native/include/ui/GraphicBuffer.h

复制代码
44class GraphicBuffer
45    : public ANativeObjectBase<ANativeWindowBuffer, GraphicBuffer, RefBase>,
46      public Flattenable<GraphicBuffer>

在其父类ANativeObjectBase中,可以看到

复制代码
33/*
34 * This helper class turns a ANativeXXX object type into a C++
35 * reference-counted object; with proper type conversions.
36 */
37template <typename NATIVE_TYPE, typename TYPE, typename REF,
38        typename NATIVE_BASE = android_native_base_t>
39class ANativeObjectBase : public NATIVE_TYPE, public REF
40{
复制代码
56    static inline TYPE* getSelf(NATIVE_TYPE* self) {
57        return static_cast<TYPE*>(self);
58    }

只是一个类型转换

参考资料:

https://zhuanlan.zhihu.com/p/30535788

相关推荐
Eastsea.Chen1 小时前
MTK Android12 user版本MtkLogger
android·framework
长亭外的少年9 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
建群新人小猿11 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神12 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
兰琛12 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法13 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
NotesChapter14 小时前
Android吸顶效果,并有着ViewPager左右切换
android
_祝你今天愉快15 小时前
分析android :The binary version of its metadata is 1.8.0, expected version is 1.5.
android
暮志未晚Webgl15 小时前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5
麦田里的守望者江16 小时前
KMP 中的 expect 和 actual 声明
android·ios·kotlin