xref: /aosp_15_r20/external/mesa3d/src/gfxstream/guest/android/GrallocGoldfish.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2023 Google LLC
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #include "GrallocGoldfish.h"
7 
8 #include <gralloc_cb_bp.h>
9 #include <vndk/hardware_buffer.h>
10 
11 namespace gfxstream {
12 
getGrallocType()13 GrallocType GoldfishGralloc::getGrallocType() { return GRALLOC_TYPE_GOLDFISH; }
14 
createColorBuffer(int width,int height,uint32_t glformat)15 uint32_t GoldfishGralloc::createColorBuffer(int width, int height, uint32_t glformat) { return 0; }
16 
allocate(uint32_t width,uint32_t height,uint32_t format,uint64_t usage,AHardwareBuffer ** outputAhb)17 int GoldfishGralloc::allocate(uint32_t width, uint32_t height, uint32_t format, uint64_t usage,
18                               AHardwareBuffer** outputAhb) {
19     struct AHardwareBuffer_Desc desc = {
20         .width = width,
21         .height = height,
22         .layers = 1,
23         .format = format,
24         .usage = usage,
25     };
26 
27     return AHardwareBuffer_allocate(&desc, outputAhb);
28 }
29 
acquire(AHardwareBuffer * ahb)30 void GoldfishGralloc::acquire(AHardwareBuffer* ahb) { AHardwareBuffer_acquire(ahb); }
31 
release(AHardwareBuffer * ahb)32 void GoldfishGralloc::release(AHardwareBuffer* ahb) { AHardwareBuffer_release(ahb); }
33 
lock(AHardwareBuffer * ahb,uint8_t ** ptr)34 int GoldfishGralloc::lock(AHardwareBuffer* ahb, uint8_t** ptr) {
35     return AHardwareBuffer_lock(ahb, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, nullptr,
36                                 reinterpret_cast<void**>(ptr));
37 }
38 
lockPlanes(AHardwareBuffer * ahb,std::vector<LockedPlane> * ahbPlanes)39 int GoldfishGralloc::lockPlanes(AHardwareBuffer* ahb, std::vector<LockedPlane>* ahbPlanes) {
40     return -1;
41 }
42 
unlock(AHardwareBuffer * ahb)43 int GoldfishGralloc::unlock(AHardwareBuffer* ahb) { return AHardwareBuffer_unlock(ahb, nullptr); }
44 
getHostHandle(native_handle_t const * handle)45 uint32_t GoldfishGralloc::getHostHandle(native_handle_t const* handle) {
46     const uint32_t INVALID_HOST_HANDLE = 0;
47 
48     const cb_handle_t* cb = cb_handle_t::from(handle);
49     if (cb) {
50         return cb->hostHandle;
51     } else {
52         return INVALID_HOST_HANDLE;
53     }
54 }
55 
getHostHandle(const AHardwareBuffer * ahb)56 uint32_t GoldfishGralloc::getHostHandle(const AHardwareBuffer* ahb) {
57     const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
58     return getHostHandle(handle);
59 }
60 
getNativeHandle(const AHardwareBuffer * ahb)61 const native_handle_t* GoldfishGralloc::getNativeHandle(const AHardwareBuffer* ahb) {
62     return AHardwareBuffer_getNativeHandle(ahb);
63 }
64 
getFormat(const native_handle_t * handle)65 int GoldfishGralloc::getFormat(const native_handle_t* handle) {
66     return cb_handle_t::from(handle)->format;
67 }
68 
getFormat(const AHardwareBuffer * ahb)69 int GoldfishGralloc::getFormat(const AHardwareBuffer* ahb) {
70     const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
71     return getFormat(handle);
72 }
73 
getFormatDrmFourcc(const native_handle_t * handle)74 uint32_t GoldfishGralloc::getFormatDrmFourcc(const native_handle_t* handle) {
75     return cb_handle_t::from(handle)->drmformat;
76 }
77 
getFormatDrmFourcc(const AHardwareBuffer * ahb)78 uint32_t GoldfishGralloc::getFormatDrmFourcc(const AHardwareBuffer* ahb) {
79     const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
80     return getFormatDrmFourcc(handle);
81 }
82 
getWidth(const AHardwareBuffer * ahb)83 uint32_t GoldfishGralloc::getWidth(const AHardwareBuffer* ahb) {
84     AHardwareBuffer_Desc desc = {};
85     AHardwareBuffer_describe(ahb, &desc);
86     return desc.width;
87 }
88 
getHeight(const AHardwareBuffer * ahb)89 uint32_t GoldfishGralloc::getHeight(const AHardwareBuffer* ahb) {
90     AHardwareBuffer_Desc desc = {};
91     AHardwareBuffer_describe(ahb, &desc);
92     return desc.height;
93 }
94 
getAllocatedSize(const native_handle_t * handle)95 size_t GoldfishGralloc::getAllocatedSize(const native_handle_t* handle) {
96     return static_cast<size_t>(cb_handle_t::from(handle)->allocatedSize());
97 }
98 
getAllocatedSize(const AHardwareBuffer * ahb)99 size_t GoldfishGralloc::getAllocatedSize(const AHardwareBuffer* ahb) {
100     const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
101     return getAllocatedSize(handle);
102 }
103 
getId(const AHardwareBuffer * ahb,uint64_t * id)104 int GoldfishGralloc::getId(const AHardwareBuffer* ahb, uint64_t* id) {
105 #if ANDROID_API_LEVEL >= 31
106     return AHardwareBuffer_getId(ahb, id);
107 #else
108     (void)ahb;
109     *id = 0;
110     return 0;
111 #endif
112 }
113 
treatBlobAsImage()114 bool GoldfishGralloc::treatBlobAsImage() { return true; }
115 
116 }  // namespace gfxstream
117