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 <queue> 19 #include <unordered_map> 20 #include <vector> 21 22 #include "gfxstream/guest/ANativeWindow.h" 23 #include "GrallocEmulated.h" 24 25 namespace gfxstream { 26 27 class EmulatedANativeWindow { 28 public: 29 EmulatedANativeWindow(uint32_t width, uint32_t height, uint32_t format, 30 std::vector<std::unique_ptr<EmulatedAHardwareBuffer>> buffers); 31 32 EGLNativeWindowType asEglNativeWindowType(); 33 34 uint32_t getWidth() const; 35 36 uint32_t getHeight() const; 37 38 int getFormat() const; 39 40 int queueBuffer(EGLClientBuffer buffer, int fence); 41 int dequeueBuffer(EGLClientBuffer* buffer, int* fence); 42 int cancelBuffer(EGLClientBuffer buffer); 43 44 void acquire(); 45 void release(); 46 47 private: 48 uint32_t mRefCount; 49 uint32_t mWidth; 50 uint32_t mHeight; 51 uint32_t mFormat; 52 std::vector<std::unique_ptr<EmulatedAHardwareBuffer>> mBuffers; 53 54 struct QueuedAHB { 55 EmulatedAHardwareBuffer* ahb; 56 int fence = -1; 57 }; 58 std::deque<QueuedAHB> mBufferQueue; 59 }; 60 61 class EmulatedANativeWindowHelper : public ANativeWindowHelper { 62 public: 63 bool isValid(EGLNativeWindowType window) override; 64 bool isValid(EGLClientBuffer buffer) override; 65 66 void acquire(EGLNativeWindowType window) override; 67 void release(EGLNativeWindowType window) override; 68 69 void acquire(EGLClientBuffer buffer) override; 70 void release(EGLClientBuffer buffer) override; 71 72 int getConsumerUsage(EGLNativeWindowType window, int* usage) override; 73 void setUsage(EGLNativeWindowType window, int usage) override; 74 75 int getWidth(EGLNativeWindowType window) override; 76 int getHeight(EGLNativeWindowType window) override; 77 78 int getWidth(EGLClientBuffer buffer) override; 79 int getHeight(EGLClientBuffer buffer) override; 80 81 int getFormat(EGLClientBuffer buffer, Gralloc* helper) override; 82 83 void setSwapInterval(EGLNativeWindowType window, int interval) override; 84 85 int queueBuffer(EGLNativeWindowType window, EGLClientBuffer buffer, int fence) override; 86 int dequeueBuffer(EGLNativeWindowType window, EGLClientBuffer* buffer, int* fence) override; 87 int cancelBuffer(EGLNativeWindowType window, EGLClientBuffer buffer) override; 88 89 int getHostHandle(EGLClientBuffer buffer, Gralloc*) override; 90 91 EGLNativeWindowType createNativeWindowForTesting(Gralloc* gralloc, uint32_t width, 92 uint32_t height) override; 93 }; 94 95 } // namespace gfxstream 96