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,
25 int height, uint32_t glformat) {
26 auto* rc = reinterpret_cast<renderControl_client_context_t*>(rcEnc);
27 return rc->rcCreateColorBuffer(rc, width, height, glformat);
28 }
29
allocate(uint32_t width,uint32_t height,uint32_t format,uint64_t usage,AHardwareBuffer ** outputAhb)30 int GoldfishGralloc::allocate(uint32_t width,
31 uint32_t height,
32 uint32_t format,
33 uint64_t usage,
34 AHardwareBuffer** outputAhb) {
35
36 struct AHardwareBuffer_Desc desc = {
37 .width = width,
38 .height = height,
39 .layers = 1,
40 .format = format,
41 .usage = usage,
42 };
43
44 return AHardwareBuffer_allocate(&desc, outputAhb);
45 }
46
acquire(AHardwareBuffer * ahb)47 void GoldfishGralloc::acquire(AHardwareBuffer* ahb) {
48 AHardwareBuffer_acquire(ahb);
49 }
50
release(AHardwareBuffer * ahb)51 void GoldfishGralloc::release(AHardwareBuffer* ahb) {
52 AHardwareBuffer_release(ahb);
53 }
54
getHostHandle(native_handle_t const * handle)55 uint32_t GoldfishGralloc::getHostHandle(native_handle_t const* handle) {
56 return cb_handle_t::from(handle)->hostHandle;
57 }
58
getHostHandle(const AHardwareBuffer * ahb)59 uint32_t GoldfishGralloc::getHostHandle(const AHardwareBuffer* ahb) {
60 const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
61 return getHostHandle(handle);
62 }
63
getFormat(const native_handle_t * handle)64 int GoldfishGralloc::getFormat(const native_handle_t* handle) {
65 return cb_handle_t::from(handle)->format;
66 }
67
getFormat(const AHardwareBuffer * ahb)68 int GoldfishGralloc::getFormat(const AHardwareBuffer* ahb) {
69 const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
70 return getFormat(handle);
71 }
72
getAllocatedSize(const native_handle_t * handle)73 size_t GoldfishGralloc::getAllocatedSize(const native_handle_t* handle) {
74 return static_cast<size_t>(cb_handle_t::from(handle)->allocatedSize());
75 }
76
getAllocatedSize(const AHardwareBuffer * ahb)77 size_t GoldfishGralloc::getAllocatedSize(const AHardwareBuffer* ahb) {
78 const native_handle_t* handle = AHardwareBuffer_getNativeHandle(ahb);
79 return getAllocatedSize(handle);
80 }
81
treatBlobAsImage()82 bool GoldfishGralloc::treatBlobAsImage() { return true; }
83
84 } // namespace gfxstream
85