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 #pragma once
16 
17 #if defined(ANDROID)
18 
19 #include <cutils/native_handle.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 typedef struct AHardwareBuffer AHardwareBuffer;
24 
25 namespace gfxstream {
26 
27 constexpr uint32_t kGlRGB = 0x1907;
28 constexpr uint32_t kGlRGBA = 0x1908;
29 constexpr uint32_t kGlRGB565 = 0x8D62;
30 
31 // Buffer pixel formats mirrored from Android to avoid extra
32 // build dependencies on Android libraries.
33 enum {
34     GFXSTREAM_AHB_FORMAT_R8G8B8A8_UNORM           = 1,
35     GFXSTREAM_AHB_FORMAT_R8G8B8X8_UNORM           = 2,
36     GFXSTREAM_AHB_FORMAT_R8G8B8_UNORM             = 3,
37     GFXSTREAM_AHB_FORMAT_R5G6B5_UNORM             = 4,
38     GFXSTREAM_AHB_FORMAT_B8G8R8A8_UNORM           = 5,
39     GFXSTREAM_AHB_FORMAT_B5G5R5A1_UNORM           = 6,
40     GFXSTREAM_AHB_FORMAT_B4G4R4A4_UNORM           = 7,
41     GFXSTREAM_AHB_FORMAT_R16G16B16A16_FLOAT       = 0x16,
42     GFXSTREAM_AHB_FORMAT_R10G10B10A2_UNORM        = 0x2b,
43     GFXSTREAM_AHB_FORMAT_BLOB                     = 0x21,
44     GFXSTREAM_AHB_FORMAT_D16_UNORM                = 0x30,
45     GFXSTREAM_AHB_FORMAT_D24_UNORM                = 0x31,
46     GFXSTREAM_AHB_FORMAT_D24_UNORM_S8_UINT        = 0x32,
47     GFXSTREAM_AHB_FORMAT_D32_FLOAT                = 0x33,
48     GFXSTREAM_AHB_FORMAT_D32_FLOAT_S8_UINT        = 0x34,
49     GFXSTREAM_AHB_FORMAT_S8_UINT                  = 0x35,
50     GFXSTREAM_AHB_FORMAT_Y8Cb8Cr8_420             = 0x23,
51     GFXSTREAM_AHB_FORMAT_YV12                     = 0x32315659,
52     GFXSTREAM_AHB_FORMAT_IMPLEMENTATION_DEFINED   = 0x22,
53     GFXSTREAM_AHB_FORMAT_R8_UNORM                 = 0x38,
54 };
55 
56 // Abstraction for gralloc handle conversion
57 class Gralloc {
58    public:
~Gralloc()59     virtual ~Gralloc() {}
60 
61     virtual uint32_t createColorBuffer(void* rcEnc, int width, int height, uint32_t glformat) = 0;
62 
63     virtual void acquire(AHardwareBuffer* ahb) = 0;
64     virtual void release(AHardwareBuffer* ahb) = 0;
65 
66     virtual int allocate(uint32_t width, uint32_t height, uint32_t ahbFormat, uint64_t usage,
67                          AHardwareBuffer** outputAhb) = 0;
68 
69     virtual int lock(AHardwareBuffer* ahb, uint8_t** ptr) = 0;
70     virtual int unlock(AHardwareBuffer* ahb) = 0;
71 
72     virtual const native_handle_t* getNativeHandle(const AHardwareBuffer* ahb) = 0;
73 
74     virtual uint32_t getHostHandle(const native_handle_t* handle) = 0;
75     virtual uint32_t getHostHandle(const AHardwareBuffer* handle) = 0;
76 
77     virtual int getFormat(const native_handle_t* handle) = 0;
78     virtual int getFormat(const AHardwareBuffer* handle) = 0;
79 
getFormatDrmFourcc(const AHardwareBuffer *)80     virtual uint32_t getFormatDrmFourcc(const AHardwareBuffer* /*handle*/) {
81         // Equal to DRM_FORMAT_INVALID -- see <drm_fourcc.h>
82         return 0;
83     }
getFormatDrmFourcc(const native_handle_t *)84     virtual uint32_t getFormatDrmFourcc(const native_handle_t* /*handle*/) {
85         // Equal to DRM_FORMAT_INVALID -- see <drm_fourcc.h>
86         return 0;
87     }
88 
89     virtual uint32_t getWidth(const AHardwareBuffer* ahb) = 0;
90     virtual uint32_t getHeight(const AHardwareBuffer* ahb) = 0;
91 
92     virtual size_t getAllocatedSize(const native_handle_t* handle) = 0;
93     virtual size_t getAllocatedSize(const AHardwareBuffer* handle) = 0;
94 
95     virtual int getId(const AHardwareBuffer* ahb, uint64_t* id) = 0;
96 
treatBlobAsImage()97     virtual bool treatBlobAsImage() { return false; }
98 };
99 
100 Gralloc* createPlatformGralloc(int deviceFd = -1);
101 
102 }  // namespace gfxstream
103 
104 #endif  // defined(ANDROID)