【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

相关推荐
Digitally21 小时前
如何用5种实用方法将电脑上的音乐传输到安卓手机
android·智能手机·电脑
HahaGiver6661 天前
Unity与Android原生交互开发入门篇 - 打开Unity游戏的设置
android·unity·交互
2501_915909061 天前
WebView 调试工具全解析,解决“看不见的移动端问题”
android·ios·小程序·https·uni-app·iphone·webview
IT乐手1 天前
android 下载管理工具类
android
2501_915106321 天前
App 怎么上架 iOS?从准备资料到开心上架(Appuploader)免 Mac 上传的完整实战流程指南
android·macos·ios·小程序·uni-app·iphone·webview
科技峰行者1 天前
安卓16提前发布能否改写移动生态格局
android
蒲公英少年带我飞1 天前
Android NDK 编译 protobuf
android
沐怡旸1 天前
【底层机制】ART虚拟机深度解析:Android运行时的架构革命
android·面试
小禾青青1 天前
uniapp安卓打包遇到报错:Uncaught SyntaxError: Invalid regular expression: /[\p{L}\p{N}]/
android·uni-app
studyForMokey1 天前
【Kotlin内联函数】
android·开发语言·kotlin