1 /*
2  * Copyright (C) 2007 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_BUFFER_MAPPER_H
18 #define ANDROID_UI_BUFFER_MAPPER_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <memory>
24 
25 #include <ui/GraphicTypes.h>
26 #include <ui/PixelFormat.h>
27 #include <ui/Rect.h>
28 #include <utils/Singleton.h>
29 
30 // Needed by code that still uses the GRALLOC_USAGE_* constants.
31 // when/if we get rid of gralloc, we should provide aliases or fix call sites.
32 #include <hardware/gralloc.h>
33 
34 
35 namespace android {
36 
37 // ---------------------------------------------------------------------------
38 
39 class GrallocMapper;
40 
41 class GraphicBufferMapper : public Singleton<GraphicBufferMapper>
42 {
43 public:
44     enum Version {
45         GRALLOC_2,
46         GRALLOC_3,
47         GRALLOC_4,
48     };
49     static void preloadHal();
get()50     static inline GraphicBufferMapper& get() { return getInstance(); }
51 
52     void dumpBuffer(buffer_handle_t bufferHandle, std::string& result, bool less = true) const;
53     static void dumpBufferToSystemLog(buffer_handle_t bufferHandle, bool less = true);
54 
55     // The imported outHandle must be freed with freeBuffer when no longer
56     // needed. rawHandle is owned by the caller.
57     status_t importBuffer(buffer_handle_t rawHandle,
58             uint32_t width, uint32_t height, uint32_t layerCount,
59             PixelFormat format, uint64_t usage, uint32_t stride,
60             buffer_handle_t* outHandle);
61 
62     status_t freeBuffer(buffer_handle_t handle);
63 
64     void getTransportSize(buffer_handle_t handle,
65             uint32_t* outTransportNumFds, uint32_t* outTransportNumInts);
66 
67     status_t lock(buffer_handle_t handle, uint32_t usage, const Rect& bounds, void** vaddr,
68                   int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr);
69 
70     status_t lockYCbCr(buffer_handle_t handle,
71             uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr);
72 
73     status_t unlock(buffer_handle_t handle);
74 
75     status_t lockAsync(buffer_handle_t handle, uint32_t usage, const Rect& bounds, void** vaddr,
76                        int fenceFd, int32_t* outBytesPerPixel = nullptr,
77                        int32_t* outBytesPerStride = nullptr);
78 
79     status_t lockAsync(buffer_handle_t handle, uint64_t producerUsage, uint64_t consumerUsage,
80                        const Rect& bounds, void** vaddr, int fenceFd,
81                        int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr);
82 
83     status_t lockAsyncYCbCr(buffer_handle_t handle,
84             uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr,
85             int fenceFd);
86 
87     status_t unlockAsync(buffer_handle_t handle, int *fenceFd);
88 
89     status_t isSupported(uint32_t width, uint32_t height, android::PixelFormat format,
90                          uint32_t layerCount, uint64_t usage, bool* outSupported);
91 
92     /**
93      * Gets the gralloc metadata associated with the buffer.
94      *
95      * These functions are supported by gralloc 4.0+.
96      */
97     status_t getBufferId(buffer_handle_t bufferHandle, uint64_t* outBufferId);
98     status_t getName(buffer_handle_t bufferHandle, std::string* outName);
99     status_t getWidth(buffer_handle_t bufferHandle, uint64_t* outWidth);
100     status_t getHeight(buffer_handle_t bufferHandle, uint64_t* outHeight);
101     status_t getLayerCount(buffer_handle_t bufferHandle, uint64_t* outLayerCount);
102     status_t getPixelFormatRequested(buffer_handle_t bufferHandle,
103                                      ui::PixelFormat* outPixelFormatRequested);
104     status_t getPixelFormatFourCC(buffer_handle_t bufferHandle, uint32_t* outPixelFormatFourCC);
105     status_t getPixelFormatModifier(buffer_handle_t bufferHandle, uint64_t* outPixelFormatModifier);
106     status_t getUsage(buffer_handle_t bufferHandle, uint64_t* outUsage);
107     status_t getAllocationSize(buffer_handle_t bufferHandle, uint64_t* outAllocationSize);
108     status_t getProtectedContent(buffer_handle_t bufferHandle, uint64_t* outProtectedContent);
109     status_t getCompression(
110             buffer_handle_t bufferHandle,
111             aidl::android::hardware::graphics::common::ExtendableType* outCompression);
112     status_t getCompression(buffer_handle_t bufferHandle, ui::Compression* outCompression);
113     status_t getInterlaced(
114             buffer_handle_t bufferHandle,
115             aidl::android::hardware::graphics::common::ExtendableType* outInterlaced);
116     status_t getInterlaced(buffer_handle_t bufferHandle, ui::Interlaced* outInterlaced);
117     status_t getChromaSiting(
118             buffer_handle_t bufferHandle,
119             aidl::android::hardware::graphics::common::ExtendableType* outChromaSiting);
120     status_t getChromaSiting(buffer_handle_t bufferHandle, ui::ChromaSiting* outChromaSiting);
121     status_t getPlaneLayouts(buffer_handle_t bufferHandle,
122                              std::vector<ui::PlaneLayout>* outPlaneLayouts);
123     status_t getDataspace(buffer_handle_t bufferHandle, ui::Dataspace* outDataspace);
124     status_t getBlendMode(buffer_handle_t bufferHandle, ui::BlendMode* outBlendMode);
125     status_t getSmpte2086(buffer_handle_t bufferHandle, std::optional<ui::Smpte2086>* outSmpte2086);
126     status_t getCta861_3(buffer_handle_t bufferHandle, std::optional<ui::Cta861_3>* outCta861_3);
127     status_t getSmpte2094_40(buffer_handle_t bufferHandle,
128                              std::optional<std::vector<uint8_t>>* outSmpte2094_40);
129 
130     /**
131      * Gets the default metadata for a gralloc buffer allocated with the given parameters.
132      *
133      * These functions are supported by gralloc 4.0+.
134      */
135     status_t getDefaultPixelFormatFourCC(uint32_t width, uint32_t height, PixelFormat format,
136                                          uint32_t layerCount, uint64_t usage,
137                                          uint32_t* outPixelFormatFourCC);
138     status_t getDefaultPixelFormatModifier(uint32_t width, uint32_t height, PixelFormat format,
139                                            uint32_t layerCount, uint64_t usage,
140                                            uint64_t* outPixelFormatModifier);
141     status_t getDefaultAllocationSize(uint32_t width, uint32_t height, PixelFormat format,
142                                       uint32_t layerCount, uint64_t usage,
143                                       uint64_t* outAllocationSize);
144     status_t getDefaultProtectedContent(uint32_t width, uint32_t height, PixelFormat format,
145                                         uint32_t layerCount, uint64_t usage,
146                                         uint64_t* outProtectedContent);
147     status_t getDefaultCompression(
148             uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount,
149             uint64_t usage,
150             aidl::android::hardware::graphics::common::ExtendableType* outCompression);
151     status_t getDefaultCompression(uint32_t width, uint32_t height, PixelFormat format,
152                                    uint32_t layerCount, uint64_t usage,
153                                    ui::Compression* outCompression);
154     status_t getDefaultInterlaced(
155             uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount,
156             uint64_t usage,
157             aidl::android::hardware::graphics::common::ExtendableType* outInterlaced);
158     status_t getDefaultInterlaced(uint32_t width, uint32_t height, PixelFormat format,
159                                   uint32_t layerCount, uint64_t usage,
160                                   ui::Interlaced* outInterlaced);
161     status_t getDefaultChromaSiting(
162             uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount,
163             uint64_t usage,
164             aidl::android::hardware::graphics::common::ExtendableType* outChromaSiting);
165     status_t getDefaultChromaSiting(uint32_t width, uint32_t height, PixelFormat format,
166                                     uint32_t layerCount, uint64_t usage,
167                                     ui::ChromaSiting* outChromaSiting);
168     status_t getDefaultPlaneLayouts(uint32_t width, uint32_t height, PixelFormat format,
169                                     uint32_t layerCount, uint64_t usage,
170                                     std::vector<ui::PlaneLayout>* outPlaneLayouts);
171 
getGrallocMapper()172     const GrallocMapper& getGrallocMapper() const {
173         return reinterpret_cast<const GrallocMapper&>(*mMapper);
174     }
175 
getMapperVersion()176     Version getMapperVersion() const { return mMapperVersion; }
177 
178 private:
179     friend class Singleton<GraphicBufferMapper>;
180 
181     GraphicBufferMapper();
182 
183     std::unique_ptr<const GrallocMapper> mMapper;
184 
185     Version mMapperVersion;
186 };
187 
188 // ---------------------------------------------------------------------------
189 
190 }; // namespace android
191 
192 #endif // ANDROID_UI_BUFFER_MAPPER_H
193 
194