1 // Copyright 2023 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 expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "GrallocGoldfish.h"
16
17 #include <gralloc_cb_bp.h>
18 #include <vndk/hardware_buffer.h>
19
20 #include "renderControl_enc.h"
21
22 namespace gfxstream {
23
createColorBuffer(void * rcEnc,int width,int height,uint32_t glformat)24 uint32_t GoldfishGralloc::createColorBuffer(void* rcEnc, int width, int height, uint32_t glformat) {
25 auto* rc = reinterpret_cast<renderControl_client_context_t*>(rcEnc);
26 return rc->rcCreateColorBuffer(rc, width, height, glformat);
27 }
28
allocate(uint32_t width,uint32_t height,uint32_t format,uint64_t usage,AHardwareBuffer ** outputAhb)29 int GoldfishGralloc::allocate(uint32_t width, uint32_t height, uint32_t format, uint64_t usage,
30 AHardwareBuffer** outputAhb) {
31 struct AHardwareBuffer_Desc desc = {
32 .width = width,
33 .height = height,
34 .layers = 1,
35 .format = format,
36 .usage = usage,
37 };
38
39 return AHardwareBuffer_allocate(&desc, outputAhb);
40 }
41
acquire(AHardwareBuffer * ahb)42 void GoldfishGralloc::acquire(AHardwareBuffer* ahb) { AHardwareBuffer_acquire(ahb); }
43
release(AHardwareBuffer * ahb)44 void GoldfishGralloc::release(AHardwareBuffer* ahb) { AHardwareBuffer_release(ahb); }
45
lock(AHardwareBuffer * ahb,uint8_t ** ptr)46 int GoldfishGralloc::lock(AHardwareBuffer* ahb, uint8_t** ptr) {
47 return AHardwareBuffer_lock(ahb, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, nullptr,
48 reinterpret_cast<void**>(ptr));
49 }
50
unlock(AHardwareBuffer * ahb)51 int GoldfishGralloc::unlock(AHardwareBuffer* ahb) { return AHardwareBuffer_unlock(ahb, nullptr); }
52
getHostHandle(native_handle_t const * handle)53 uint32_t GoldfishGralloc::getHostHandle(native_handle_t const* handle) {
54 const uint32_t INVALID_HOST_HANDLE = 0;
55
56 const cb_handle_t* cb = cb_handle_t::from(handle);
57 if (cb) {
58 return cb->hostHandle;
59 } else {
60 return INVALID_HOST_HANDLE;
61 }
62 }
63
getHostHandle(const AHardwareBuffer * ahb)64 uint32_t GoldfishGralloc::getHostHandle(const AHardwareBuffer* ahb) {
65 const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
66 return getHostHandle(handle);
67 }
68
getNativeHandle(const AHardwareBuffer * ahb)69 const native_handle_t* GoldfishGralloc::getNativeHandle(const AHardwareBuffer* ahb) {
70 return AHardwareBuffer_getNativeHandle(ahb);
71 }
72
getFormat(const native_handle_t * handle)73 int GoldfishGralloc::getFormat(const native_handle_t* handle) {
74 return cb_handle_t::from(handle)->format;
75 }
76
getFormat(const AHardwareBuffer * ahb)77 int GoldfishGralloc::getFormat(const AHardwareBuffer* ahb) {
78 const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
79 return getFormat(handle);
80 }
81
getFormatDrmFourcc(const native_handle_t * handle)82 uint32_t GoldfishGralloc::getFormatDrmFourcc(const native_handle_t* handle) {
83 return cb_handle_t::from(handle)->drmformat;
84 }
85
getFormatDrmFourcc(const AHardwareBuffer * ahb)86 uint32_t GoldfishGralloc::getFormatDrmFourcc(const AHardwareBuffer* ahb) {
87 const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
88 return getFormatDrmFourcc(handle);
89 }
90
getWidth(const AHardwareBuffer * ahb)91 uint32_t GoldfishGralloc::getWidth(const AHardwareBuffer* ahb) {
92 AHardwareBuffer_Desc desc = {};
93 AHardwareBuffer_describe(ahb, &desc);
94 return desc.width;
95 }
96
getHeight(const AHardwareBuffer * ahb)97 uint32_t GoldfishGralloc::getHeight(const AHardwareBuffer* ahb) {
98 AHardwareBuffer_Desc desc = {};
99 AHardwareBuffer_describe(ahb, &desc);
100 return desc.height;
101 }
102
getAllocatedSize(const native_handle_t * handle)103 size_t GoldfishGralloc::getAllocatedSize(const native_handle_t* handle) {
104 return static_cast<size_t>(cb_handle_t::from(handle)->allocatedSize());
105 }
106
getAllocatedSize(const AHardwareBuffer * ahb)107 size_t GoldfishGralloc::getAllocatedSize(const AHardwareBuffer* ahb) {
108 const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
109 return getAllocatedSize(handle);
110 }
111
getId(const AHardwareBuffer * ahb,uint64_t * id)112 int GoldfishGralloc::getId(const AHardwareBuffer* ahb, uint64_t* id) {
113 #if ANDROID_API_LEVEL >= 31
114 return AHardwareBuffer_getId(ahb, id);
115 #else
116 (void)ahb;
117 *id = 0;
118 return 0;
119 #endif
120 }
121
treatBlobAsImage()122 bool GoldfishGralloc::treatBlobAsImage() { return true; }
123
124 } // namespace gfxstream
125