第4章:Resource抽象层 4.1 资源管理框架Resource(资源)是virglrenderer中另一个核心抽象。资源代表GPU可访问的内存对象,如纹理、缓冲区、渲染目标等。与Context类似,资源也采用了统一的抽象层设计。struct virgl_resource设计资源是全局的、跨Context的对象。一个资源可以被多个Context共享使用,但必须先通过attach_resource绑定到Context,才能在该Context中使用。完整的Resource结构定义/* src/virgl_resource.h *//** * A global cross-context resource. A virgl_resource is not directly usable * by renderer contexts, but must be attached and imported into renderer * contexts to create context objects first. For example, it can be attached * and imported into a vrend_decode_ctx to create a vrend_resource. * * It is also possible to create a virgl_resource from a context object. * * The underlying storage of a virgl_resource is provided by a pipe_resource * and/or a fd. When it is provided by a pipe_resource, the virgl_resource is * said to be typed because pipe_resource also provides the type information. * * Conventional resources are always typed. Blob resources by definition do * not have nor need type information, but those created from vrend_decode_ctx * objects are typed. That should be considered a convenience rather than * something to be relied upon. Contexts must not assume that every resource is * typed when interop is expected. */structvirgl_resource{/* ========== 标识 ========== */uint32_tres_id;// 全局唯一资源ID/* ========== 存储方式 ========== *//* 类型化资源(Typed Resource) */structpipe_resource*pipe_resource;// Mesa pipe资源(包含类型信息)/* 文件描述符资源 */enumvirgl_resource_fd_typefd_type;// FD类型intfd;// 文件描述符/* Opaque Handle资源 */uint32_topaque_handle_context_id;// 创建此资源的Context IDuint32_topaque_handle;// 不透明句柄/* 虚拟地址资源 */void*va_handle;// 虚拟地址指针/* ========== Guest内存 ========== */conststructiovec*iov;// Guest内存的scatter-gather列表intiov_count;// iovec数量/* ========== 映射信息 ========== */uint32_tmap_info;// 缓存属性等uint64_tmap_size;// 映射大小void*mapped;// 映射后的Host地址bool mapped_from_pipe_resource;// 是否从pipe_resource映射/* ========== Vulkan特定 ========== */structvirgl_resource_vulkan_infovulkan_info;// Vulkan资源信息/* ========== 扩展数据 ========== */void*private_data;// 私有数据指针};资源FD类型enumvirgl_resource_fd_type{/* DMA-BUF:可以在进程间共享的GPU缓冲区 */VIRGL_RESOURCE_FD_DMABUF,/* Opaque FD:后端特定的不透明文件描述符 */VIRGL_RESOURCE_FD_OPAQUE,/* 共享内存:可mmap的memfd或shm */VIRGL_RESOURCE_FD_SHM,/* Opaque Handle:类似GEM handle,可按需导出为fd */VIRGL_RESOURCE_OPAQUE_HANDLE,/* 虚拟地址:基于VA的资源,无需map */VIRGL_RESOURCE_VA_HANDLE,/* 无效类型 */VIRGL_RESOURCE_FD_INVALID=-1,};Vulkan资源信息structvirgl_resource_vulkan_info{uint8_tdevice_uuid[16];// 设备UUIDuint8_tdriver_uuid[16];// 驱动UUIDuint64_tallocation_size;// 分配大小uint32_tmemory_type_index;// 内存类型索引};资源与Context的关系┌──────────────────────────────────────────────────────────┐ │ 全局资源表 (Global Resource Table) │ │ │ │ ┌────────────┐ ┌────────────┐ ┌────────────┐ │ │ │Resource A │ │Resource B │ │Resource C │ ... │ │ │res_id=100 │ │res_id=200 │ │res_id=300 │ │ │ └──────┬─────┘ └──────┬─────┘ └──────┬─────┘ │ └─────────┼────────────────┼────────────────┼──────────────┘ │ │ │ │ attach │ attach │ attach ▼ ▼ ▼ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ Context 1 │ │ Context 2 │ │ Context 3 │ │ │ │ │ │ │ │ Local Resources:│ │ Local Resources:│ │ Local Resources:│ │ • A' (from A) │ │ • B' (from B) │ │ • A'' (from A) │ │ • B' (from B) │ │ • C' (from C) │ │ • C'' (from C) │ └─────────────────┘ └─────────────────┘ └─────────────────┘关键点:全局资源:virgl_resource是全局的,存储在全局哈希表中Context局部资源:每个Context通过attach_resource创建自己的局部资源对象共享机制:同一个全局资源可以被多个Context绑定生命周期:全局资源的生命周期独立于Context资源管理的回调机制框架提供了pipe资源的回调接口,供VIRGL后端使用:structvirgl_resource_pipe_callbacks{void*data;/* 释放pipe资源引用 */void(*unref)(structpipe_resource*pres,void*data);/* 绑定/解绑Guest内存 */void(*attach_iov)(structpipe_resource*pres,conststructiovec*iov,intiov_count,void*data);void(*detach_iov)(structpipe_resource*pres,void*data);/* 导出为文件描述符 */enumvirgl_resource_fd_type(*export_fd)(structpipe_resource*pres,int*fd,void*data);};4.2 资源类型Virglrenderer支持多种资源类型,以适应不同的使用场景和性能需求。1. 传统资源(Conventional Resources)特点:总是类型化的(typed)通过virgl_renderer_resource_create创建包含完整的格式信息(纹理格式、维度等)创建参数:structvirgl_renderer_resource_create_args{uint32_thandle;// 资源句柄uint32_ttarget;// 目标类型(TEXTURE_2D, BUFFER等)uint32_tformat;// 像素格式(RGBA8, DEPTH24等)uint32_tbind;// 绑定标志(RENDER_TARGET, SAMPLER等)uint32_twidth;// 宽度uint32_theight;// 高度uint32_tdepth;// 深度uint32_tarray_size;// 数组大小uint32_tlast_level;// Mipmap层级uint32_tnr_samples;// 多重采样数uint32_tflags;// 额外标志};绑定标志示例:#defineVIRGL_RES_BIND_DEPTH_STENCIL(10)// 深度/模板缓冲#defineVIRGL_RES_BIND_RENDER_TARGET(11)// 渲染目标#defineVIRGL_RES_BIND_SAMPLER_VIEW(13)// 采样器视图#defineVIRGL_RES_BIND_VERTEX_BUFFER(14)// 顶点缓冲#defineVIRGL_RES_BIND_INDEX_BUFFER(15)// 索引缓冲#defineVIRGL_RES_BIND_CONSTANT_BUFFER(16)// 常量缓冲#defineVIRGL_RES_BIND_SCANOUT(118)// 扫描输出#defi