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