1 // Copyright 2024 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <cstdint> 16 #include <memory> 17 #include <optional> 18 #include <vector> 19 20 #include "VirtGpu.h" 21 #include "gfxstream/guest/Gralloc.h" 22 23 namespace gfxstream { 24 25 using EGLClientBuffer = void*; 26 27 class EmulatedAHardwareBuffer { 28 public: 29 EmulatedAHardwareBuffer(uint32_t width, uint32_t height, uint32_t drmFormat, 30 VirtGpuResourcePtr resource); 31 32 ~EmulatedAHardwareBuffer(); 33 34 uint32_t getResourceId() const; 35 36 uint32_t getWidth() const; 37 38 uint32_t getHeight() const; 39 40 int getAndroidFormat() const; 41 42 uint32_t getDrmFormat() const; 43 44 AHardwareBuffer* asAHardwareBuffer(); 45 46 buffer_handle_t asBufferHandle(); 47 48 EGLClientBuffer asEglClientBuffer(); 49 50 void acquire(); 51 void release(); 52 53 int lock(uint8_t** ptr); 54 int unlock(); 55 56 private: 57 uint32_t mRefCount; 58 uint32_t mWidth; 59 uint32_t mHeight; 60 uint32_t mDrmFormat; 61 VirtGpuResourcePtr mResource; 62 std::optional<VirtGpuResourceMappingPtr> mMapped; 63 }; 64 65 class EmulatedGralloc : public Gralloc { 66 public: 67 EmulatedGralloc(); 68 69 uint32_t createColorBuffer(void*, int width, int height, uint32_t glFormat) override; 70 71 int allocate(uint32_t width, uint32_t height, uint32_t format, uint64_t usage, 72 AHardwareBuffer** outputAhb) override; 73 74 AHardwareBuffer* allocate(uint32_t width, uint32_t height, uint32_t format); 75 76 void acquire(AHardwareBuffer* ahb) override; 77 void release(AHardwareBuffer* ahb) override; 78 79 int lock(AHardwareBuffer* ahb, uint8_t** ptr) override; 80 int unlock(AHardwareBuffer* ahb) override; 81 82 uint32_t getHostHandle(const native_handle_t* handle) override; 83 uint32_t getHostHandle(const AHardwareBuffer* handle) override; 84 85 const native_handle_t* getNativeHandle(const AHardwareBuffer* ahb) override; 86 87 int getFormat(const native_handle_t* handle) override; 88 int getFormat(const AHardwareBuffer* handle) override; 89 90 uint32_t getFormatDrmFourcc(const AHardwareBuffer* handle) override; 91 92 uint32_t getWidth(const AHardwareBuffer* ahb) override; 93 uint32_t getHeight(const AHardwareBuffer* ahb) override; 94 95 size_t getAllocatedSize(const native_handle_t*) override; 96 size_t getAllocatedSize(const AHardwareBuffer*) override; 97 98 int getId(const AHardwareBuffer* ahb, uint64_t* id) override; 99 100 private: 101 std::vector<std::unique_ptr<EmulatedAHardwareBuffer>> mOwned; 102 }; 103 104 } // namespace gfxstream 105