1 /* 2 * Copyright 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef VIRTGPU_DEVICE_H 17 #define VIRTGPU_DEVICE_H 18 19 #include <cstdint> 20 #include <memory> 21 22 #include "virtgpu_gfxstream_protocol.h" 23 24 // See virgl_hw.h and p_defines.h 25 #define VIRGL_FORMAT_R8_UNORM 64 26 #define VIRGL_FORMAT_B8G8R8A8_UNORM 1 27 #define VIRGL_FORMAT_B5G6R5_UNORM 7 28 #define VIRGL_FORMAT_R8G8B8_UNORM 66 29 #define VIRGL_FORMAT_R8G8B8A8_UNORM 67 30 31 #define VIRGL_BIND_RENDER_TARGET (1 << 1) 32 #define VIRGL_BIND_CUSTOM (1 << 17) 33 #define VIRGL_BIND_LINEAR (1 << 22) 34 35 #define PIPE_BUFFER 0 36 #define PIPE_TEXTURE_2D 2 37 38 enum VirtGpuParamId : uint32_t { 39 kParam3D = 0, 40 kParamCapsetFix = 1, 41 kParamResourceBlob = 2, 42 kParamHostVisible = 3, 43 kParamCrossDevice = 4, 44 kParamContextInit = 5, 45 kParamSupportedCapsetIds = 6, 46 kParamExplicitDebugName = 7, 47 kParamCreateGuestHandle = 8, 48 kParamMax = 9, 49 }; 50 51 enum VirtGpuExecBufferFlags : uint32_t { 52 kFenceIn = 0x0001, 53 kFenceOut = 0x0002, 54 kRingIdx = 0x0004, 55 }; 56 57 enum VirtGpuCapset { 58 kCapsetNone = 0, 59 kCapsetVirgl = 1, 60 kCapsetVirgl2 = 2, 61 kCapsetGfxStreamVulkan = 3, 62 kCapsetVenus = 4, 63 kCapsetCrossDomain = 5, 64 kCapsetDrm = 6, 65 kCapsetGfxStreamMagma = 7, 66 kCapsetGfxStreamGles = 8, 67 kCapsetGfxStreamComposer = 9, 68 }; 69 70 // Try to keep aligned with vulkan-cereal / rutabaga. 71 enum VirtGpuHandleType { 72 kMemHandleOpaqueFd = 0x0001, 73 kMemHandleDmabuf = 0x0002, 74 kMemHandleOpaqueWin32 = 0x0003, 75 kMemHandleShm = 0x0004, 76 kMemHandleZircon = 0x0008, 77 kFenceHandleOpaqueFd = 0x0010, 78 kFenceHandleSyncFd = 0x0020, 79 kFenceHandleOpaqueWin32 = 0x0040, 80 kFenceHandleZircon = 0x0080, 81 }; 82 83 enum VirtGpuResourceFlags : uint32_t { 84 kBlobFlagMappable = 0x0001, 85 kBlobFlagShareable = 0x0002, 86 kBlobFlagCrossDevice = 0x0004, 87 kBlobFlagCreateGuestHandle = 0x0008, 88 }; 89 90 enum VirtGpuResourceMem { 91 kBlobMemGuest = 0x0001, 92 kBlobMemHost3d = 0x0002, 93 kBlobMemHost3dGuest = 0x0003, 94 }; 95 96 struct VirtGpuExternalHandle { 97 int64_t osHandle; 98 enum VirtGpuHandleType type; 99 }; 100 101 struct VirtGpuExecBuffer { 102 void* command; 103 uint32_t command_size; 104 uint32_t ring_idx; 105 enum VirtGpuExecBufferFlags flags; 106 struct VirtGpuExternalHandle handle; 107 }; 108 109 struct VirtGpuParam { 110 uint64_t param; 111 const char* name; 112 uint64_t value; 113 }; 114 115 struct VirtGpuCreateBlob { 116 uint64_t size; 117 enum VirtGpuResourceFlags flags; 118 enum VirtGpuResourceMem blobMem; 119 uint64_t blobId; 120 }; 121 122 struct VirtGpuCaps { 123 uint64_t params[kParamMax]; 124 struct vulkanCapset vulkanCapset; 125 struct magmaCapset magmaCapset; 126 struct glesCapset glesCapset; 127 struct composerCapset composerCapset; 128 }; 129 130 class VirtGpuResourceMapping; 131 class VirtGpuResource; 132 using VirtGpuResourcePtr = std::shared_ptr<VirtGpuResource>; 133 using VirtGpuResourceMappingPtr = std::shared_ptr<VirtGpuResourceMapping>; 134 135 class VirtGpuResource { 136 public: ~VirtGpuResource()137 virtual ~VirtGpuResource() {} 138 139 virtual uint32_t getResourceHandle() const = 0; 140 virtual uint32_t getBlobHandle() const = 0; 141 virtual int wait() = 0; 142 143 virtual VirtGpuResourceMappingPtr createMapping(void) = 0; 144 virtual int exportBlob(struct VirtGpuExternalHandle& handle) = 0; 145 146 virtual int transferFromHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) = 0; transferFromHost(uint32_t offset,uint32_t size)147 virtual int transferFromHost(uint32_t offset, uint32_t size) { 148 return transferFromHost(offset, 0, size, 1); 149 } 150 151 virtual int transferToHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) = 0; transferToHost(uint32_t offset,uint32_t size)152 virtual int transferToHost(uint32_t offset, uint32_t size) { 153 return transferToHost(offset, 0, size, 1); 154 } 155 }; 156 157 class VirtGpuResourceMapping { 158 public: ~VirtGpuResourceMapping(void)159 virtual ~VirtGpuResourceMapping(void) {} 160 161 virtual uint8_t* asRawPtr(void) = 0; 162 }; 163 164 class VirtGpuDevice { 165 public: 166 static VirtGpuDevice* getInstance(enum VirtGpuCapset capset = kCapsetNone); 167 static void resetInstance(); 168 169 static void setInstanceForTesting(VirtGpuDevice* device); 170 VirtGpuDevice(enum VirtGpuCapset capset)171 VirtGpuDevice(enum VirtGpuCapset capset) : mCapset(capset) {} ~VirtGpuDevice()172 virtual ~VirtGpuDevice() {} 173 capset()174 enum VirtGpuCapset capset() { return mCapset; } 175 176 virtual int64_t getDeviceHandle(void) = 0; 177 178 virtual struct VirtGpuCaps getCaps(void) = 0; 179 180 virtual VirtGpuResourcePtr createBlob(const struct VirtGpuCreateBlob& blobCreate) = 0; 181 virtual VirtGpuResourcePtr createResource(uint32_t width, uint32_t height, uint32_t stride, 182 uint32_t virglFormat, uint32_t target, 183 uint32_t bind) = 0; 184 virtual VirtGpuResourcePtr importBlob(const struct VirtGpuExternalHandle& handle) = 0; 185 186 virtual int execBuffer(struct VirtGpuExecBuffer& execbuffer, const VirtGpuResource* blob) = 0; 187 188 private: 189 enum VirtGpuCapset mCapset; 190 }; 191 192 VirtGpuDevice* createPlatformVirtGpuDevice(enum VirtGpuCapset capset = kCapsetNone, int fd = -1); 193 194 // HACK: We can use gfxstream::guest::EnumFlags, but we'll have to do more guest 195 // refactorings to figure out our end goal. We can either depend more on base or 196 // try to transition to something else (b:202552093) [atleast for guests]. 197 constexpr enum VirtGpuResourceFlags operator|(const enum VirtGpuResourceFlags self, 198 const enum VirtGpuResourceFlags other) { 199 return (enum VirtGpuResourceFlags)(uint32_t(self) | uint32_t(other)); 200 } 201 202 constexpr enum VirtGpuExecBufferFlags operator |(const enum VirtGpuExecBufferFlags self, 203 const enum VirtGpuExecBufferFlags other) { 204 return (enum VirtGpuExecBufferFlags)(uint32_t(self) | uint32_t(other)); 205 } 206 207 #endif 208