先定义一个宏:
cpp
#ifndef container_of
#define container_of(ptr, type, member) \
(type *)((char*)(ptr) - offsetof(type, member))
#endif
然后:
cpp
ANativeWindowBuffer *anwBuffer = container_of(buffer, ANativeWindowBuffer, handle);
其中buffer的类型是:
cpp
buffer_handle_t *buffer;
得到ANativeWindowBuffer以后:
cpp
sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
return static_cast<GraphicBuffer *>(anwb);
}