xref: /aosp_15_r20/external/mesa3d/src/gfxstream/guest/android/GrallocEmulated.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 <vector>
10 
11 #include "VirtGpu.h"
12 #include "gfxstream/guest/GfxStreamGralloc.h"
13 
14 namespace gfxstream {
15 
16 using EGLClientBuffer = void*;
17 
18 class EmulatedAHardwareBuffer {
19    public:
20     EmulatedAHardwareBuffer(uint32_t width, uint32_t height, uint32_t drmFormat,
21                             VirtGpuResourcePtr resource);
22 
23     ~EmulatedAHardwareBuffer();
24 
25     uint32_t getResourceId() const;
26 
27     uint32_t getWidth() const;
28 
29     uint32_t getHeight() const;
30 
31     int getAndroidFormat() const;
32 
33     uint32_t getDrmFormat() const;
34 
35     AHardwareBuffer* asAHardwareBuffer();
36 
37     buffer_handle_t asBufferHandle();
38 
39     EGLClientBuffer asEglClientBuffer();
40 
41     void acquire();
42     void release();
43 
44     int lock(uint8_t** ptr);
45     int lockPlanes(std::vector<Gralloc::LockedPlane>* ahbPlanes);
46     int unlock();
47 
48    private:
49     uint32_t mRefCount;
50     uint32_t mWidth;
51     uint32_t mHeight;
52     uint32_t mDrmFormat;
53     VirtGpuResourcePtr mResource;
54     std::optional<VirtGpuResourceMappingPtr> mMapped;
55 };
56 
57 class EmulatedGralloc : public Gralloc {
58    public:
59     EmulatedGralloc(int32_t descriptor);
60     ~EmulatedGralloc();
61 
62     GrallocType getGrallocType() override;
63     uint32_t createColorBuffer(int width, int height, uint32_t glFormat) override;
64 
65     int allocate(uint32_t width, uint32_t height, uint32_t format, uint64_t usage,
66                  AHardwareBuffer** outputAhb) override;
67 
68     AHardwareBuffer* allocate(uint32_t width, uint32_t height, uint32_t format);
69 
70     void acquire(AHardwareBuffer* ahb) override;
71     void release(AHardwareBuffer* ahb) override;
72 
73     int lock(AHardwareBuffer* ahb, uint8_t** ptr) override;
74     int lockPlanes(AHardwareBuffer* ahb, std::vector<LockedPlane>* ahbPlanes) override;
75     int unlock(AHardwareBuffer* ahb) override;
76 
77     uint32_t getHostHandle(const native_handle_t* handle) override;
78     uint32_t getHostHandle(const AHardwareBuffer* handle) override;
79 
80     const native_handle_t* getNativeHandle(const AHardwareBuffer* ahb) override;
81 
82     int getFormat(const native_handle_t* handle) override;
83     int getFormat(const AHardwareBuffer* handle) override;
84 
85     uint32_t getFormatDrmFourcc(const AHardwareBuffer* handle) override;
86 
87     uint32_t getWidth(const AHardwareBuffer* ahb) override;
88     uint32_t getHeight(const AHardwareBuffer* ahb) override;
89 
90     size_t getAllocatedSize(const native_handle_t*) override;
91     size_t getAllocatedSize(const AHardwareBuffer*) override;
92 
93     int getId(const AHardwareBuffer* ahb, uint64_t* id) override;
94 
95    private:
96     std::unique_ptr<VirtGpuDevice> mDevice;
97     std::vector<std::unique_ptr<EmulatedAHardwareBuffer>> mOwned;
98 };
99 
100 }  // namespace gfxstream
101