1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_UI_GRALLOC4_H
18 #define ANDROID_UI_GRALLOC4_H
19 
20 #include <android/hardware/graphics/allocator/4.0/IAllocator.h>
21 #include <android/hardware/graphics/common/1.1/types.h>
22 #include <android/hardware/graphics/mapper/4.0/IMapper.h>
23 #include <gralloctypes/Gralloc4.h>
24 #include <ui/Gralloc.h>
25 #include <ui/GraphicTypes.h>
26 #include <ui/Rect.h>
27 #include <utils/StrongPointer.h>
28 
29 #include <string>
30 
31 namespace android {
32 
33 class Gralloc4Mapper : public GrallocMapper {
34 public:
35     static void preload();
36 
37     Gralloc4Mapper();
38 
39     bool isLoaded() const override;
40 
41     std::string dumpBuffer(buffer_handle_t bufferHandle, bool less = true) const override;
42     std::string dumpBuffers(bool less = true) const;
43 
44     status_t createDescriptor(void* bufferDescriptorInfo, void* outBufferDescriptor) const override;
45 
46     status_t importBuffer(const hardware::hidl_handle& rawHandle,
47                           buffer_handle_t* outBufferHandle) const override;
48 
49     void freeBuffer(buffer_handle_t bufferHandle) const override;
50 
51     status_t validateBufferSize(buffer_handle_t bufferHandle, uint32_t width, uint32_t height,
52                                 PixelFormat format, uint32_t layerCount, uint64_t usage,
53                                 uint32_t stride) const override;
54 
55     void getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds,
56                           uint32_t* outNumInts) const override;
57 
58     status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
59                   int acquireFence, void** outData, int32_t* outBytesPerPixel,
60                   int32_t* outBytesPerStride) const override;
61 
62     status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
63                   int acquireFence, android_ycbcr* ycbcr) const override;
64 
65     int unlock(buffer_handle_t bufferHandle) const override;
66 
67     status_t isSupported(uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount,
68                          uint64_t usage, bool* outSupported) const override;
69 
70     status_t getBufferId(buffer_handle_t bufferHandle, uint64_t* outBufferId) const override;
71     status_t getName(buffer_handle_t bufferHandle, std::string* outName) const override;
72     status_t getWidth(buffer_handle_t bufferHandle, uint64_t* outWidth) const override;
73     status_t getHeight(buffer_handle_t bufferHandle, uint64_t* outHeight) const override;
74     status_t getLayerCount(buffer_handle_t bufferHandle, uint64_t* outLayerCount) const override;
75     status_t getPixelFormatRequested(buffer_handle_t bufferHandle,
76                                      ui::PixelFormat* outPixelFormatRequested) const override;
77     status_t getPixelFormatFourCC(buffer_handle_t bufferHandle,
78                                   uint32_t* outPixelFormatFourCC) const override;
79     status_t getPixelFormatModifier(buffer_handle_t bufferHandle,
80                                     uint64_t* outPixelFormatModifier) const override;
81     status_t getUsage(buffer_handle_t bufferHandle, uint64_t* outUsage) const override;
82     status_t getAllocationSize(buffer_handle_t bufferHandle,
83                                uint64_t* outAllocationSize) const override;
84     status_t getProtectedContent(buffer_handle_t bufferHandle,
85                                  uint64_t* outProtectedContent) const override;
86     status_t getCompression(buffer_handle_t bufferHandle,
87                             aidl::android::hardware::graphics::common::ExtendableType*
88                                     outCompression) const override;
89     status_t getCompression(buffer_handle_t bufferHandle,
90                             ui::Compression* outCompression) const override;
91     status_t getInterlaced(buffer_handle_t bufferHandle,
92                            aidl::android::hardware::graphics::common::ExtendableType* outInterlaced)
93             const override;
94     status_t getInterlaced(buffer_handle_t bufferHandle,
95                            ui::Interlaced* outInterlaced) const override;
96     status_t getChromaSiting(buffer_handle_t bufferHandle,
97                              aidl::android::hardware::graphics::common::ExtendableType*
98                                      outChromaSiting) const override;
99     status_t getChromaSiting(buffer_handle_t bufferHandle,
100                              ui::ChromaSiting* outChromaSiting) const override;
101     status_t getPlaneLayouts(buffer_handle_t bufferHandle,
102                              std::vector<ui::PlaneLayout>* outPlaneLayouts) const override;
103     status_t getDataspace(buffer_handle_t bufferHandle, ui::Dataspace* outDataspace) const override;
104     status_t getBlendMode(buffer_handle_t bufferHandle, ui::BlendMode* outBlendMode) const override;
105     status_t getSmpte2086(buffer_handle_t bufferHandle,
106                           std::optional<ui::Smpte2086>* outSmpte2086) const override;
107     status_t getCta861_3(buffer_handle_t bufferHandle,
108                          std::optional<ui::Cta861_3>* outCta861_3) const override;
109     status_t getSmpte2094_40(buffer_handle_t bufferHandle,
110                              std::optional<std::vector<uint8_t>>* outSmpte2094_40) const override;
111 
112     status_t getDefaultPixelFormatFourCC(uint32_t width, uint32_t height, PixelFormat format,
113                                          uint32_t layerCount, uint64_t usage,
114                                          uint32_t* outPixelFormatFourCC) const override;
115     status_t getDefaultPixelFormatModifier(uint32_t width, uint32_t height, PixelFormat format,
116                                            uint32_t layerCount, uint64_t usage,
117                                            uint64_t* outPixelFormatModifier) const override;
118     status_t getDefaultAllocationSize(uint32_t width, uint32_t height, PixelFormat format,
119                                       uint32_t layerCount, uint64_t usage,
120                                       uint64_t* outAllocationSize) const override;
121     status_t getDefaultProtectedContent(uint32_t width, uint32_t height, PixelFormat format,
122                                         uint32_t layerCount, uint64_t usage,
123                                         uint64_t* outProtectedContent) const override;
124     status_t getDefaultCompression(uint32_t width, uint32_t height, PixelFormat format,
125                                    uint32_t layerCount, uint64_t usage,
126                                    aidl::android::hardware::graphics::common::ExtendableType*
127                                            outCompression) const override;
128     status_t getDefaultCompression(uint32_t width, uint32_t height, PixelFormat format,
129                                    uint32_t layerCount, uint64_t usage,
130                                    ui::Compression* outCompression) const override;
131     status_t getDefaultInterlaced(uint32_t width, uint32_t height, PixelFormat format,
132                                   uint32_t layerCount, uint64_t usage,
133                                   aidl::android::hardware::graphics::common::ExtendableType*
134                                           outInterlaced) const override;
135     status_t getDefaultInterlaced(uint32_t width, uint32_t height, PixelFormat format,
136                                   uint32_t layerCount, uint64_t usage,
137                                   ui::Interlaced* outInterlaced) const override;
138     status_t getDefaultChromaSiting(uint32_t width, uint32_t height, PixelFormat format,
139                                     uint32_t layerCount, uint64_t usage,
140                                     aidl::android::hardware::graphics::common::ExtendableType*
141                                             outChromaSiting) const override;
142     status_t getDefaultChromaSiting(uint32_t width, uint32_t height, PixelFormat format,
143                                     uint32_t layerCount, uint64_t usage,
144                                     ui::ChromaSiting* outChromaSiting) const override;
145     status_t getDefaultPlaneLayouts(uint32_t width, uint32_t height, PixelFormat format,
146                                     uint32_t layerCount, uint64_t usage,
147                                     std::vector<ui::PlaneLayout>* outPlaneLayouts) const override;
148 
149     std::vector<android::hardware::graphics::mapper::V4_0::IMapper::MetadataTypeDescription>
150     listSupportedMetadataTypes() const;
151 
152 private:
153     friend class GraphicBufferAllocator;
154 
155     // Determines whether the passed info is compatible with the mapper.
156     status_t validateBufferDescriptorInfo(
157             hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo* descriptorInfo) const;
158 
159     template <class T>
160     using DecodeFunction = status_t (*)(const hardware::hidl_vec<uint8_t>& input, T* output);
161 
162     template <class T>
163     status_t get(
164             buffer_handle_t bufferHandle,
165             const android::hardware::graphics::mapper::V4_0::IMapper::MetadataType& metadataType,
166             DecodeFunction<T> decodeFunction, T* outMetadata) const;
167 
168     template <class T>
169     status_t getDefault(
170             uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount,
171             uint64_t usage,
172             const android::hardware::graphics::mapper::V4_0::IMapper::MetadataType& metadataType,
173             DecodeFunction<T> decodeFunction, T* outMetadata) const;
174 
175     template <class T>
176     status_t metadataDumpHelper(
177             const android::hardware::graphics::mapper::V4_0::IMapper::BufferDump& bufferDump,
178             aidl::android::hardware::graphics::common::StandardMetadataType metadataType,
179             DecodeFunction<T> decodeFunction, T* outT) const;
180     status_t bufferDumpHelper(
181             const android::hardware::graphics::mapper::V4_0::IMapper::BufferDump& bufferDump,
182             std::ostringstream* outDump, uint64_t* outAllocationSize, bool less) const;
183 
184     sp<hardware::graphics::mapper::V4_0::IMapper> mMapper;
185 };
186 
187 class Gralloc4Allocator : public GrallocAllocator {
188 public:
189     // An allocator relies on a mapper, and that mapper must be alive at all
190     // time.
191     Gralloc4Allocator(const Gralloc4Mapper& mapper);
192 
193     bool isLoaded() const override;
194 
195     std::string dumpDebugInfo(bool less = true) const override;
196 
197     status_t allocate(std::string requestorName, uint32_t width, uint32_t height,
198                       PixelFormat format, uint32_t layerCount, uint64_t usage, uint32_t bufferCount,
199                       uint32_t* outStride, buffer_handle_t* outBufferHandles,
200                       bool importBuffers = true) const override;
201 
202 private:
203     const Gralloc4Mapper& mMapper;
204     sp<hardware::graphics::allocator::V4_0::IAllocator> mAllocator;
205 };
206 
207 } // namespace android
208 
209 #endif // ANDROID_UI_GRALLOC4_H
210