1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <aidl/android/hardware/graphics/allocator/IAllocator.h>
20 #include <android/hardware/graphics/mapper/IMapper.h>
21 #include <ui/Gralloc.h>
22 
23 namespace android {
24 
25 class Gralloc5Mapper : public GrallocMapper {
26 public:
27 public:
28     static void preload();
29 
30     Gralloc5Mapper();
31 
32     [[nodiscard]] bool isLoaded() const override;
33 
34     [[nodiscard]] std::string dumpBuffer(buffer_handle_t bufferHandle, bool less) const override;
35 
36     [[nodiscard]] std::string dumpBuffers(bool less = true) const;
37 
38     [[nodiscard]] status_t importBuffer(const native_handle_t *rawHandle,
39                                         buffer_handle_t *outBufferHandle) const override;
40 
41     void freeBuffer(buffer_handle_t bufferHandle) const override;
42 
43     [[nodiscard]] status_t validateBufferSize(buffer_handle_t bufferHandle, uint32_t width,
44                                               uint32_t height, PixelFormat format,
45                                               uint32_t layerCount, uint64_t usage,
46                                               uint32_t stride) const override;
47 
48     void getTransportSize(buffer_handle_t bufferHandle, uint32_t *outNumFds,
49                           uint32_t *outNumInts) const override;
50 
51     [[nodiscard]] status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect &bounds,
52                                 int acquireFence, void **outData, int32_t *outBytesPerPixel,
53                                 int32_t *outBytesPerStride) const override;
54 
55     [[nodiscard]] status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect &bounds,
56                                 int acquireFence, android_ycbcr *ycbcr) const override;
57 
58     [[nodiscard]] int unlock(buffer_handle_t bufferHandle) const override;
59 
60     [[nodiscard]] status_t isSupported(uint32_t width, uint32_t height, PixelFormat format,
61                                        uint32_t layerCount, uint64_t usage,
62                                        bool *outSupported) const override;
63 
64     [[nodiscard]] status_t getBufferId(buffer_handle_t bufferHandle,
65                                        uint64_t *outBufferId) const override;
66 
67     [[nodiscard]] status_t getName(buffer_handle_t bufferHandle,
68                                    std::string *outName) const override;
69 
70     [[nodiscard]] status_t getWidth(buffer_handle_t bufferHandle,
71                                     uint64_t *outWidth) const override;
72 
73     [[nodiscard]] status_t getHeight(buffer_handle_t bufferHandle,
74                                      uint64_t *outHeight) const override;
75 
76     [[nodiscard]] status_t getLayerCount(buffer_handle_t bufferHandle,
77                                          uint64_t *outLayerCount) const override;
78 
79     [[nodiscard]] status_t getPixelFormatRequested(
80             buffer_handle_t bufferHandle, ui::PixelFormat *outPixelFormatRequested) const override;
81 
82     [[nodiscard]] status_t getPixelFormatFourCC(buffer_handle_t bufferHandle,
83                                                 uint32_t *outPixelFormatFourCC) const override;
84 
85     [[nodiscard]] status_t getPixelFormatModifier(buffer_handle_t bufferHandle,
86                                                   uint64_t *outPixelFormatModifier) const override;
87 
88     [[nodiscard]] status_t getUsage(buffer_handle_t bufferHandle,
89                                     uint64_t *outUsage) const override;
90 
91     [[nodiscard]] status_t getAllocationSize(buffer_handle_t bufferHandle,
92                                              uint64_t *outAllocationSize) const override;
93 
94     [[nodiscard]] status_t getProtectedContent(buffer_handle_t bufferHandle,
95                                                uint64_t *outProtectedContent) const override;
96 
97     [[nodiscard]] status_t getCompression(buffer_handle_t bufferHandle,
98                                           aidl::android::hardware::graphics::common::ExtendableType
99                                                   *outCompression) const override;
100 
101     [[nodiscard]] status_t getCompression(buffer_handle_t bufferHandle,
102                                           ui::Compression *outCompression) const override;
103 
104     [[nodiscard]] status_t getInterlaced(buffer_handle_t bufferHandle,
105                                          aidl::android::hardware::graphics::common::ExtendableType
106                                                  *outInterlaced) const override;
107 
108     [[nodiscard]] status_t getInterlaced(buffer_handle_t bufferHandle,
109                                          ui::Interlaced *outInterlaced) const override;
110 
111     [[nodiscard]] status_t getChromaSiting(buffer_handle_t bufferHandle,
112                                            aidl::android::hardware::graphics::common::ExtendableType
113                                                    *outChromaSiting) const override;
114 
115     [[nodiscard]] status_t getChromaSiting(buffer_handle_t bufferHandle,
116                                            ui::ChromaSiting *outChromaSiting) const override;
117 
118     [[nodiscard]] status_t getPlaneLayouts(
119             buffer_handle_t bufferHandle,
120             std::vector<ui::PlaneLayout> *outPlaneLayouts) const override;
121 
122     [[nodiscard]] status_t getDataspace(buffer_handle_t bufferHandle,
123                                         ui::Dataspace *outDataspace) const override;
124 
125     [[nodiscard]] status_t setDataspace(buffer_handle_t bufferHandle,
126                                         ui::Dataspace dataspace) const override;
127 
128     [[nodiscard]] status_t getBlendMode(buffer_handle_t bufferHandle,
129                                         ui::BlendMode *outBlendMode) const override;
130 
131     [[nodiscard]] status_t getSmpte2086(buffer_handle_t bufferHandle,
132                                         std::optional<ui::Smpte2086> *outSmpte2086) const override;
133 
134     [[nodiscard]] status_t setSmpte2086(buffer_handle_t bufferHandle,
135                                         std::optional<ui::Smpte2086> smpte2086) const override;
136 
137     [[nodiscard]] status_t getCta861_3(buffer_handle_t bufferHandle,
138                                        std::optional<ui::Cta861_3> *outCta861_3) const override;
139 
140     [[nodiscard]] status_t setCta861_3(buffer_handle_t bufferHandle,
141                                        std::optional<ui::Cta861_3> cta861_3) const override;
142 
143     [[nodiscard]] status_t getSmpte2094_40(
144             buffer_handle_t bufferHandle,
145             std::optional<std::vector<uint8_t>> *outSmpte2094_40) const override;
146 
147     [[nodiscard]] status_t setSmpte2094_40(
148             buffer_handle_t bufferHandle,
149             std::optional<std::vector<uint8_t>> smpte2094_40) const override;
150 
151     [[nodiscard]] status_t getSmpte2094_10(
152             buffer_handle_t bufferHandle,
153             std::optional<std::vector<uint8_t>> *outSmpte2094_10) const override;
154 
155     [[nodiscard]] status_t setSmpte2094_10(
156             buffer_handle_t bufferHandle,
157             std::optional<std::vector<uint8_t>> smpte2094_10) const override;
158 
159 private:
160     void unlockBlocking(buffer_handle_t bufferHandle) const;
161 
162     AIMapper *mMapper = nullptr;
163 };
164 
165 class Gralloc5Allocator : public GrallocAllocator {
166 public:
167     Gralloc5Allocator(const Gralloc5Mapper &mapper);
168 
169     [[nodiscard]] bool isLoaded() const override;
170 
171     [[nodiscard]] std::string dumpDebugInfo(bool less) const override;
172 
173     [[nodiscard]] status_t allocate(std::string requestorName, uint32_t width, uint32_t height,
174                                     PixelFormat format, uint32_t layerCount, uint64_t usage,
175                                     uint32_t* outStride, buffer_handle_t* outBufferHandles,
176                                     bool importBuffers) const override;
177 
178     [[nodiscard]] GraphicBufferAllocator::AllocationResult allocate(
179             const GraphicBufferAllocator::AllocationRequest&) const override;
180 
supportsAdditionalOptions()181     bool supportsAdditionalOptions() const override { return true; }
182 
183 private:
184     const Gralloc5Mapper &mMapper;
185     std::shared_ptr<aidl::android::hardware::graphics::allocator::IAllocator> mAllocator;
186 };
187 
188 } // namespace android
189