• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <aidl/android/hardware/graphics/allocator/IAllocator.h>
21 #include <android/hardware/graphics/allocator/4.0/IAllocator.h>
22 #include <android/hardware/graphics/common/1.1/types.h>
23 #include <android/hardware/graphics/mapper/4.0/IMapper.h>
24 #include <gralloctypes/Gralloc4.h>
25 #include <ui/Gralloc.h>
26 #include <ui/GraphicTypes.h>
27 #include <ui/Rect.h>
28 #include <utils/StrongPointer.h>
29 
30 #include <string>
31 
32 namespace android {
33 
34 class Gralloc4Mapper : public GrallocMapper {
35 public:
36     static void preload();
37 
38     Gralloc4Mapper();
39 
40     bool isLoaded() const override;
41 
42     std::string dumpBuffer(buffer_handle_t bufferHandle, bool less = true) const override;
43     std::string dumpBuffers(bool less = true) const;
44 
45     status_t createDescriptor(void* bufferDescriptorInfo, void* outBufferDescriptor) const override;
46 
47     status_t importBuffer(const hardware::hidl_handle& rawHandle,
48                           buffer_handle_t* outBufferHandle) const override;
49 
50     void freeBuffer(buffer_handle_t bufferHandle) const override;
51 
52     status_t validateBufferSize(buffer_handle_t bufferHandle, uint32_t width, uint32_t height,
53                                 PixelFormat format, uint32_t layerCount, uint64_t usage,
54                                 uint32_t stride) const override;
55 
56     void getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds,
57                           uint32_t* outNumInts) const override;
58 
59     status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
60                   int acquireFence, void** outData, int32_t* outBytesPerPixel,
61                   int32_t* outBytesPerStride) const override;
62 
63     status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
64                   int acquireFence, android_ycbcr* ycbcr) const override;
65 
66     int unlock(buffer_handle_t bufferHandle) const override;
67 
68     status_t isSupported(uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount,
69                          uint64_t usage, bool* outSupported) const override;
70 
71     status_t getBufferId(buffer_handle_t bufferHandle, uint64_t* outBufferId) const override;
72     status_t getName(buffer_handle_t bufferHandle, std::string* outName) const override;
73     status_t getWidth(buffer_handle_t bufferHandle, uint64_t* outWidth) const override;
74     status_t getHeight(buffer_handle_t bufferHandle, uint64_t* outHeight) const override;
75     status_t getLayerCount(buffer_handle_t bufferHandle, uint64_t* outLayerCount) const override;
76     status_t getPixelFormatRequested(buffer_handle_t bufferHandle,
77                                      ui::PixelFormat* outPixelFormatRequested) const override;
78     status_t getPixelFormatFourCC(buffer_handle_t bufferHandle,
79                                   uint32_t* outPixelFormatFourCC) const override;
80     status_t getPixelFormatModifier(buffer_handle_t bufferHandle,
81                                     uint64_t* outPixelFormatModifier) const override;
82     status_t getUsage(buffer_handle_t bufferHandle, uint64_t* outUsage) const override;
83     status_t getAllocationSize(buffer_handle_t bufferHandle,
84                                uint64_t* outAllocationSize) const override;
85     status_t getProtectedContent(buffer_handle_t bufferHandle,
86                                  uint64_t* outProtectedContent) const override;
87     status_t getCompression(buffer_handle_t bufferHandle,
88                             aidl::android::hardware::graphics::common::ExtendableType*
89                                     outCompression) const override;
90     status_t getCompression(buffer_handle_t bufferHandle,
91                             ui::Compression* outCompression) const override;
92     status_t getInterlaced(buffer_handle_t bufferHandle,
93                            aidl::android::hardware::graphics::common::ExtendableType* outInterlaced)
94             const override;
95     status_t getInterlaced(buffer_handle_t bufferHandle,
96                            ui::Interlaced* outInterlaced) const override;
97     status_t getChromaSiting(buffer_handle_t bufferHandle,
98                              aidl::android::hardware::graphics::common::ExtendableType*
99                                      outChromaSiting) const override;
100     status_t getChromaSiting(buffer_handle_t bufferHandle,
101                              ui::ChromaSiting* outChromaSiting) const override;
102     status_t getPlaneLayouts(buffer_handle_t bufferHandle,
103                              std::vector<ui::PlaneLayout>* outPlaneLayouts) const override;
104     status_t getDataspace(buffer_handle_t bufferHandle, ui::Dataspace* outDataspace) const override;
105     status_t setDataspace(buffer_handle_t bufferHandle, ui::Dataspace dataspace) const override;
106     status_t getBlendMode(buffer_handle_t bufferHandle, ui::BlendMode* outBlendMode) const override;
107     status_t getSmpte2086(buffer_handle_t bufferHandle,
108                           std::optional<ui::Smpte2086>* outSmpte2086) const override;
109     status_t setSmpte2086(buffer_handle_t bufferHandle,
110                           std::optional<ui::Smpte2086> smpte2086) const override;
111     status_t getCta861_3(buffer_handle_t bufferHandle,
112                          std::optional<ui::Cta861_3>* outCta861_3) const override;
113     status_t setCta861_3(buffer_handle_t bufferHandle,
114                          std::optional<ui::Cta861_3> cta861_3) const override;
115     status_t getSmpte2094_40(buffer_handle_t bufferHandle,
116                              std::optional<std::vector<uint8_t>>* outSmpte2094_40) const override;
117     status_t setSmpte2094_40(buffer_handle_t bufferHandle,
118                              std::optional<std::vector<uint8_t>> smpte2094_40) const override;
119     status_t getSmpte2094_10(buffer_handle_t bufferHandle,
120                              std::optional<std::vector<uint8_t>>* outSmpte2094_10) const override;
121     status_t setSmpte2094_10(buffer_handle_t bufferHandle,
122                              std::optional<std::vector<uint8_t>> smpte2094_10) const override;
123     status_t getDefaultPixelFormatFourCC(uint32_t width, uint32_t height, PixelFormat format,
124                                          uint32_t layerCount, uint64_t usage,
125                                          uint32_t* outPixelFormatFourCC) const override;
126     status_t getDefaultPixelFormatModifier(uint32_t width, uint32_t height, PixelFormat format,
127                                            uint32_t layerCount, uint64_t usage,
128                                            uint64_t* outPixelFormatModifier) const override;
129     status_t getDefaultAllocationSize(uint32_t width, uint32_t height, PixelFormat format,
130                                       uint32_t layerCount, uint64_t usage,
131                                       uint64_t* outAllocationSize) const override;
132     status_t getDefaultProtectedContent(uint32_t width, uint32_t height, PixelFormat format,
133                                         uint32_t layerCount, uint64_t usage,
134                                         uint64_t* outProtectedContent) const override;
135     status_t getDefaultCompression(uint32_t width, uint32_t height, PixelFormat format,
136                                    uint32_t layerCount, uint64_t usage,
137                                    aidl::android::hardware::graphics::common::ExtendableType*
138                                            outCompression) const override;
139     status_t getDefaultCompression(uint32_t width, uint32_t height, PixelFormat format,
140                                    uint32_t layerCount, uint64_t usage,
141                                    ui::Compression* outCompression) const override;
142     status_t getDefaultInterlaced(uint32_t width, uint32_t height, PixelFormat format,
143                                   uint32_t layerCount, uint64_t usage,
144                                   aidl::android::hardware::graphics::common::ExtendableType*
145                                           outInterlaced) const override;
146     status_t getDefaultInterlaced(uint32_t width, uint32_t height, PixelFormat format,
147                                   uint32_t layerCount, uint64_t usage,
148                                   ui::Interlaced* outInterlaced) const override;
149     status_t getDefaultChromaSiting(uint32_t width, uint32_t height, PixelFormat format,
150                                     uint32_t layerCount, uint64_t usage,
151                                     aidl::android::hardware::graphics::common::ExtendableType*
152                                             outChromaSiting) const override;
153     status_t getDefaultChromaSiting(uint32_t width, uint32_t height, PixelFormat format,
154                                     uint32_t layerCount, uint64_t usage,
155                                     ui::ChromaSiting* outChromaSiting) const override;
156     status_t getDefaultPlaneLayouts(uint32_t width, uint32_t height, PixelFormat format,
157                                     uint32_t layerCount, uint64_t usage,
158                                     std::vector<ui::PlaneLayout>* outPlaneLayouts) const override;
159 
160     std::vector<android::hardware::graphics::mapper::V4_0::IMapper::MetadataTypeDescription>
161     listSupportedMetadataTypes() const;
162 
163 private:
164     friend class GraphicBufferAllocator;
165 
166     template <class T>
167     using DecodeFunction = status_t (*)(const hardware::hidl_vec<uint8_t>& input, T* output);
168     template <class T>
169     using EncodeFunction = status_t (*)(const T& input, hardware::hidl_vec<uint8_t>* output);
170 
171     template <class T>
172     status_t get(
173             buffer_handle_t bufferHandle,
174             const android::hardware::graphics::mapper::V4_0::IMapper::MetadataType& metadataType,
175             DecodeFunction<T> decodeFunction, T* outMetadata) const;
176 
177     template <class T>
178     status_t set(
179             buffer_handle_t bufferHandle,
180             const android::hardware::graphics::mapper::V4_0::IMapper::MetadataType& metadataType,
181             const T& metadata, EncodeFunction<T> encodeFunction) const;
182 
183     template <class T>
184     status_t getDefault(
185             uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount,
186             uint64_t usage,
187             const android::hardware::graphics::mapper::V4_0::IMapper::MetadataType& metadataType,
188             DecodeFunction<T> decodeFunction, T* outMetadata) const;
189 
190     template <class T>
191     status_t metadataDumpHelper(
192             const android::hardware::graphics::mapper::V4_0::IMapper::BufferDump& bufferDump,
193             aidl::android::hardware::graphics::common::StandardMetadataType metadataType,
194             DecodeFunction<T> decodeFunction, T* outT) const;
195     status_t bufferDumpHelper(
196             const android::hardware::graphics::mapper::V4_0::IMapper::BufferDump& bufferDump,
197             std::ostringstream* outDump, uint64_t* outAllocationSize, bool less) const;
198 
199     sp<hardware::graphics::mapper::V4_0::IMapper> mMapper;
200 };
201 
202 class Gralloc4Allocator : public GrallocAllocator {
203 public:
204     // An allocator relies on a mapper, and that mapper must be alive at all
205     // time.
206     Gralloc4Allocator(const Gralloc4Mapper& mapper);
207 
208     bool isLoaded() const override;
209 
210     std::string dumpDebugInfo(bool less = true) const override;
211 
212     status_t allocate(std::string requestorName, uint32_t width, uint32_t height,
213                       PixelFormat format, uint32_t layerCount, uint64_t usage, uint32_t bufferCount,
214                       uint32_t* outStride, buffer_handle_t* outBufferHandles,
215                       bool importBuffers = true) const override;
216 
217 private:
218     const Gralloc4Mapper& mMapper;
219     sp<hardware::graphics::allocator::V4_0::IAllocator> mAllocator;
220     // Optional "4.1" allocator
221     std::shared_ptr<aidl::android::hardware::graphics::allocator::IAllocator> mAidlAllocator;
222 };
223 
224 } // namespace android
225 
226 #endif // ANDROID_UI_GRALLOC4_H
227