【Android】GraphicBuffer和ANativeWindowBuffer

GraphicBuffer是surface的内存数据载体,

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

//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处理进行了转换,

    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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值