1 /* 2 * Copyright 2023 Google LLC 3 * SPDX-License-Identifier: Apache-2.0 4 */ 5 6 #include <optional> 7 #include <queue> 8 #include <unordered_map> 9 #include <vector> 10 11 #include "VirtGpu.h" 12 #include "ANativeWindow.h" 13 14 namespace gfxstream { 15 16 class TestingAHardwareBuffer { 17 public: 18 TestingAHardwareBuffer(uint32_t width, 19 uint32_t height, 20 VirtGpuBlobPtr resource); 21 22 ~TestingAHardwareBuffer(); 23 24 uint32_t getResourceId() const; 25 26 uint32_t getWidth() const; 27 28 uint32_t getHeight() const; 29 30 int getAndroidFormat() const; 31 32 uint32_t getDrmFormat() const; 33 34 AHardwareBuffer* asAHardwareBuffer(); 35 36 buffer_handle_t asBufferHandle(); 37 38 EGLClientBuffer asEglClientBuffer(); 39 40 private: 41 uint32_t mWidth; 42 uint32_t mHeight; 43 VirtGpuBlobPtr mResource; 44 native_handle_t *mHandle = nullptr; 45 }; 46 47 class TestingVirtGpuGralloc : public Gralloc { 48 public: 49 TestingVirtGpuGralloc(); 50 51 uint32_t createColorBuffer(void*, 52 int width, 53 int height, 54 uint32_t glFormat) override; 55 56 int allocate(uint32_t width, 57 uint32_t height, 58 uint32_t format, 59 uint64_t usage, 60 AHardwareBuffer** outputAhb) override; 61 62 std::unique_ptr<TestingAHardwareBuffer> allocate(uint32_t width, 63 uint32_t height, 64 uint32_t format); 65 66 void acquire(AHardwareBuffer* ahb) override; 67 void release(AHardwareBuffer* ahb) override; 68 69 uint32_t getHostHandle(const native_handle_t* handle) override; 70 uint32_t getHostHandle(const AHardwareBuffer* handle) override; 71 72 int getFormat(const native_handle_t* handle) override; 73 int getFormat(const AHardwareBuffer* handle) override; 74 75 uint32_t getFormatDrmFourcc(const AHardwareBuffer* handle) override; 76 77 size_t getAllocatedSize(const native_handle_t*) override; 78 size_t getAllocatedSize(const AHardwareBuffer*) override; 79 80 private: 81 std::unordered_map<uint32_t, std::unique_ptr<TestingAHardwareBuffer>> mAllocatedColorBuffers; 82 }; 83 84 class TestingANativeWindow { 85 public: 86 TestingANativeWindow(uint32_t width, 87 uint32_t height, 88 uint32_t format, 89 std::vector<std::unique_ptr<TestingAHardwareBuffer>> buffers); 90 91 EGLNativeWindowType asEglNativeWindowType(); 92 93 uint32_t getWidth() const; 94 95 uint32_t getHeight() const; 96 97 int getFormat() const; 98 99 int queueBuffer(EGLClientBuffer buffer, int fence); 100 int dequeueBuffer(EGLClientBuffer* buffer, int* fence); 101 int cancelBuffer(EGLClientBuffer buffer); 102 103 private: 104 uint32_t mWidth; 105 uint32_t mHeight; 106 uint32_t mFormat; 107 std::vector<std::unique_ptr<TestingAHardwareBuffer>> mBuffers; 108 109 struct QueuedAHB { 110 TestingAHardwareBuffer* ahb; 111 int fence = -1; 112 }; 113 std::deque<QueuedAHB> mBufferQueue; 114 }; 115 116 class TestingVirtGpuANativeWindowHelper : public ANativeWindowHelper { 117 public: 118 bool isValid(EGLNativeWindowType window) override; 119 bool isValid(EGLClientBuffer buffer) override; 120 121 void acquire(EGLNativeWindowType window) override; 122 void release(EGLNativeWindowType window) override; 123 124 void acquire(EGLClientBuffer buffer) override; 125 void release(EGLClientBuffer buffer) override; 126 127 int getConsumerUsage(EGLNativeWindowType window, int* usage) override; 128 void setUsage(EGLNativeWindowType window, int usage) override; 129 130 int getWidth(EGLNativeWindowType window) override; 131 int getHeight(EGLNativeWindowType window) override; 132 133 int getWidth(EGLClientBuffer buffer) override; 134 int getHeight(EGLClientBuffer buffer) override; 135 136 int getFormat(EGLClientBuffer buffer, Gralloc* helper) override; 137 138 void setSwapInterval(EGLNativeWindowType window, int interval) override; 139 140 int queueBuffer(EGLNativeWindowType window, EGLClientBuffer buffer, int fence) override; 141 int dequeueBuffer(EGLNativeWindowType window, EGLClientBuffer* buffer, int* fence) override; 142 int cancelBuffer(EGLNativeWindowType window, EGLClientBuffer buffer) override; 143 144 int getHostHandle(EGLClientBuffer buffer, Gralloc*) override; 145 }; 146 147 } // namespace gfxstream 148