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 }
只是一个类型转换
参考资料: