xref: /aosp_15_r20/external/mesa3d/src/gfxstream/guest/android/ANativeWindowEmulated.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2024 Google LLC
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #include <cstdint>
7 #include <memory>
8 #include <optional>
9 #include <queue>
10 #include <unordered_map>
11 #include <vector>
12 
13 #include "gfxstream/guest/ANativeWindow.h"
14 #include "GrallocEmulated.h"
15 
16 namespace gfxstream {
17 
18 class EmulatedANativeWindow {
19    public:
20     EmulatedANativeWindow(uint32_t width, uint32_t height, uint32_t format,
21                           std::vector<std::unique_ptr<EmulatedAHardwareBuffer>> buffers);
22 
23     EGLNativeWindowType asEglNativeWindowType();
24 
25     uint32_t getWidth() const;
26 
27     uint32_t getHeight() const;
28 
29     int getFormat() const;
30 
31     int queueBuffer(EGLClientBuffer buffer, int fence);
32     int dequeueBuffer(EGLClientBuffer* buffer, int* fence);
33     int cancelBuffer(EGLClientBuffer buffer);
34 
35     void acquire();
36     void release();
37 
38    private:
39     uint32_t mRefCount;
40     uint32_t mWidth;
41     uint32_t mHeight;
42     uint32_t mFormat;
43     std::vector<std::unique_ptr<EmulatedAHardwareBuffer>> mBuffers;
44 
45     struct QueuedAHB {
46         EmulatedAHardwareBuffer* ahb;
47         int fence = -1;
48     };
49     std::deque<QueuedAHB> mBufferQueue;
50 };
51 
52 class EmulatedANativeWindowHelper : public ANativeWindowHelper {
53    public:
54     bool isValid(EGLNativeWindowType window) override;
55     bool isValid(EGLClientBuffer buffer) override;
56 
57     void acquire(EGLNativeWindowType window) override;
58     void release(EGLNativeWindowType window) override;
59 
60     void acquire(EGLClientBuffer buffer) override;
61     void release(EGLClientBuffer buffer) override;
62 
63     int getConsumerUsage(EGLNativeWindowType window, int* usage) override;
64     void setUsage(EGLNativeWindowType window, int usage) override;
65 
66     int getWidth(EGLNativeWindowType window) override;
67     int getHeight(EGLNativeWindowType window) override;
68 
69     int getWidth(EGLClientBuffer buffer) override;
70     int getHeight(EGLClientBuffer buffer) override;
71 
72     int getFormat(EGLClientBuffer buffer, Gralloc* helper) override;
73 
74     void setSwapInterval(EGLNativeWindowType window, int interval) override;
75 
76     int queueBuffer(EGLNativeWindowType window, EGLClientBuffer buffer, int fence) override;
77     int dequeueBuffer(EGLNativeWindowType window, EGLClientBuffer* buffer, int* fence) override;
78     int cancelBuffer(EGLNativeWindowType window, EGLClientBuffer buffer) override;
79 
80     int getHostHandle(EGLClientBuffer buffer, Gralloc*) override;
81 
82     EGLNativeWindowType createNativeWindowForTesting(Gralloc* gralloc, uint32_t width,
83                                                      uint32_t height) override;
84 };
85 
86 }  // namespace gfxstream
87