• 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;
46 
47     status_t importBuffer(const native_handle_t* 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 
124     std::vector<android::hardware::graphics::mapper::V4_0::IMapper::MetadataTypeDescription>
125     listSupportedMetadataTypes() const;
126 
127 private:
128     friend class GraphicBufferAllocator;
129 
130     template <class T>
131     using DecodeFunction = status_t (*)(const hardware::hidl_vec<uint8_t>& input, T* output);
132     template <class T>
133     using EncodeFunction = status_t (*)(const T& input, hardware::hidl_vec<uint8_t>* output);
134 
135     template <class T>
136     status_t get(
137             buffer_handle_t bufferHandle,
138             const android::hardware::graphics::mapper::V4_0::IMapper::MetadataType& metadataType,
139             DecodeFunction<T> decodeFunction, T* outMetadata) const;
140 
141     template <class T>
142     status_t set(
143             buffer_handle_t bufferHandle,
144             const android::hardware::graphics::mapper::V4_0::IMapper::MetadataType& metadataType,
145             const T& metadata, EncodeFunction<T> encodeFunction) const;
146 
147     template <class T>
148     status_t getDefault(
149             uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount,
150             uint64_t usage,
151             const android::hardware::graphics::mapper::V4_0::IMapper::MetadataType& metadataType,
152             DecodeFunction<T> decodeFunction, T* outMetadata) const;
153 
154     template <class T>
155     status_t metadataDumpHelper(
156             const android::hardware::graphics::mapper::V4_0::IMapper::BufferDump& bufferDump,
157             aidl::android::hardware::graphics::common::StandardMetadataType metadataType,
158             DecodeFunction<T> decodeFunction, T* outT) const;
159     status_t bufferDumpHelper(
160             const android::hardware::graphics::mapper::V4_0::IMapper::BufferDump& bufferDump,
161             std::ostringstream* outDump, uint64_t* outAllocationSize, bool less) const;
162 
163     sp<hardware::graphics::mapper::V4_0::IMapper> mMapper;
164 };
165 
166 class Gralloc4Allocator : public GrallocAllocator {
167 public:
168     // An allocator relies on a mapper, and that mapper must be alive at all
169     // time.
170     Gralloc4Allocator(const Gralloc4Mapper& mapper);
171 
172     bool isLoaded() const override;
173 
174     std::string dumpDebugInfo(bool less = true) const override;
175 
176     status_t allocate(std::string requestorName, uint32_t width, uint32_t height,
177                       PixelFormat format, uint32_t layerCount, uint64_t usage, uint32_t* outStride,
178                       buffer_handle_t* outBufferHandles, bool importBuffers = true) const override;
179 
180 private:
181     const Gralloc4Mapper& mMapper;
182     sp<hardware::graphics::allocator::V4_0::IAllocator> mAllocator;
183     // Optional "4.1" allocator
184     std::shared_ptr<aidl::android::hardware::graphics::allocator::IAllocator> mAidlAllocator;
185 };
186 
187 } // namespace android
188 
189 #endif // ANDROID_UI_GRALLOC4_H
190