• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "GraphicBufferMapper"
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 //#define LOG_NDEBUG 0
20 
21 #include <ui/GraphicBufferMapper.h>
22 
23 #include <grallocusage/GrallocUsageConversion.h>
24 
25 // We would eliminate the non-conforming zero-length array, but we can't since
26 // this is effectively included from the Linux kernel
27 #pragma clang diagnostic push
28 #pragma clang diagnostic ignored "-Wzero-length-array"
29 #include <sync/sync.h>
30 #pragma clang diagnostic pop
31 
32 #include <utils/Log.h>
33 #include <utils/Trace.h>
34 
35 #include <ui/Gralloc.h>
36 #include <ui/Gralloc2.h>
37 #include <ui/Gralloc3.h>
38 #include <ui/Gralloc4.h>
39 #include <ui/GraphicBuffer.h>
40 
41 #include <system/graphics.h>
42 
43 namespace android {
44 // ---------------------------------------------------------------------------
45 
ANDROID_SINGLETON_STATIC_INSTANCE(GraphicBufferMapper)46 ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
47 
48 void GraphicBufferMapper::preloadHal() {
49     Gralloc2Mapper::preload();
50     Gralloc3Mapper::preload();
51     Gralloc4Mapper::preload();
52 }
53 
GraphicBufferMapper()54 GraphicBufferMapper::GraphicBufferMapper() {
55     mMapper = std::make_unique<const Gralloc4Mapper>();
56     if (mMapper->isLoaded()) {
57         mMapperVersion = Version::GRALLOC_4;
58         return;
59     }
60     mMapper = std::make_unique<const Gralloc3Mapper>();
61     if (mMapper->isLoaded()) {
62         mMapperVersion = Version::GRALLOC_3;
63         return;
64     }
65     mMapper = std::make_unique<const Gralloc2Mapper>();
66     if (mMapper->isLoaded()) {
67         mMapperVersion = Version::GRALLOC_2;
68         return;
69     }
70 
71     LOG_ALWAYS_FATAL("gralloc-mapper is missing");
72 }
73 
dumpBuffer(buffer_handle_t bufferHandle,std::string & result,bool less) const74 void GraphicBufferMapper::dumpBuffer(buffer_handle_t bufferHandle, std::string& result,
75                                      bool less) const {
76     result.append(mMapper->dumpBuffer(bufferHandle, less));
77 }
78 
dumpBufferToSystemLog(buffer_handle_t bufferHandle,bool less)79 void GraphicBufferMapper::dumpBufferToSystemLog(buffer_handle_t bufferHandle, bool less) {
80     std::string s;
81     GraphicBufferMapper::getInstance().dumpBuffer(bufferHandle, s, less);
82     ALOGD("%s", s.c_str());
83 }
84 
importBuffer(buffer_handle_t rawHandle,uint32_t width,uint32_t height,uint32_t layerCount,PixelFormat format,uint64_t usage,uint32_t stride,buffer_handle_t * outHandle)85 status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle,
86         uint32_t width, uint32_t height, uint32_t layerCount,
87         PixelFormat format, uint64_t usage, uint32_t stride,
88         buffer_handle_t* outHandle)
89 {
90     ATRACE_CALL();
91 
92     buffer_handle_t bufferHandle;
93     status_t error = mMapper->importBuffer(hardware::hidl_handle(rawHandle), &bufferHandle);
94     if (error != NO_ERROR) {
95         ALOGW("importBuffer(%p) failed: %d", rawHandle, error);
96         return error;
97     }
98 
99     error = mMapper->validateBufferSize(bufferHandle, width, height, format, layerCount, usage,
100                                         stride);
101     if (error != NO_ERROR) {
102         ALOGE("validateBufferSize(%p) failed: %d", rawHandle, error);
103         freeBuffer(bufferHandle);
104         return static_cast<status_t>(error);
105     }
106 
107     *outHandle = bufferHandle;
108 
109     return NO_ERROR;
110 }
111 
getTransportSize(buffer_handle_t handle,uint32_t * outTransportNumFds,uint32_t * outTransportNumInts)112 void GraphicBufferMapper::getTransportSize(buffer_handle_t handle,
113             uint32_t* outTransportNumFds, uint32_t* outTransportNumInts)
114 {
115     mMapper->getTransportSize(handle, outTransportNumFds, outTransportNumInts);
116 }
117 
freeBuffer(buffer_handle_t handle)118 status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
119 {
120     ATRACE_CALL();
121 
122     mMapper->freeBuffer(handle);
123 
124     return NO_ERROR;
125 }
126 
lock(buffer_handle_t handle,uint32_t usage,const Rect & bounds,void ** vaddr,int32_t * outBytesPerPixel,int32_t * outBytesPerStride)127 status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
128                                    void** vaddr, int32_t* outBytesPerPixel,
129                                    int32_t* outBytesPerStride) {
130     return lockAsync(handle, usage, bounds, vaddr, -1, outBytesPerPixel, outBytesPerStride);
131 }
132 
lockYCbCr(buffer_handle_t handle,uint32_t usage,const Rect & bounds,android_ycbcr * ycbcr)133 status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
134         const Rect& bounds, android_ycbcr *ycbcr)
135 {
136     return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
137 }
138 
unlock(buffer_handle_t handle)139 status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
140 {
141     int32_t fenceFd = -1;
142     status_t error = unlockAsync(handle, &fenceFd);
143     if (error == NO_ERROR && fenceFd >= 0) {
144         sync_wait(fenceFd, -1);
145         close(fenceFd);
146     }
147     return error;
148 }
149 
lockAsync(buffer_handle_t handle,uint32_t usage,const Rect & bounds,void ** vaddr,int fenceFd,int32_t * outBytesPerPixel,int32_t * outBytesPerStride)150 status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
151                                         void** vaddr, int fenceFd, int32_t* outBytesPerPixel,
152                                         int32_t* outBytesPerStride) {
153     return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd, outBytesPerPixel,
154                      outBytesPerStride);
155 }
156 
lockAsync(buffer_handle_t handle,uint64_t producerUsage,uint64_t consumerUsage,const Rect & bounds,void ** vaddr,int fenceFd,int32_t * outBytesPerPixel,int32_t * outBytesPerStride)157 status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint64_t producerUsage,
158                                         uint64_t consumerUsage, const Rect& bounds, void** vaddr,
159                                         int fenceFd, int32_t* outBytesPerPixel,
160                                         int32_t* outBytesPerStride) {
161     ATRACE_CALL();
162 
163     const uint64_t usage = static_cast<uint64_t>(
164             android_convertGralloc1To0Usage(producerUsage, consumerUsage));
165     return mMapper->lock(handle, usage, bounds, fenceFd, vaddr, outBytesPerPixel,
166                          outBytesPerStride);
167 }
168 
lockAsyncYCbCr(buffer_handle_t handle,uint32_t usage,const Rect & bounds,android_ycbcr * ycbcr,int fenceFd)169 status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
170         uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
171 {
172     ATRACE_CALL();
173 
174     return mMapper->lock(handle, usage, bounds, fenceFd, ycbcr);
175 }
176 
unlockAsync(buffer_handle_t handle,int * fenceFd)177 status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
178 {
179     ATRACE_CALL();
180 
181     *fenceFd = mMapper->unlock(handle);
182 
183     return NO_ERROR;
184 }
185 
isSupported(uint32_t width,uint32_t height,android::PixelFormat format,uint32_t layerCount,uint64_t usage,bool * outSupported)186 status_t GraphicBufferMapper::isSupported(uint32_t width, uint32_t height,
187                                           android::PixelFormat format, uint32_t layerCount,
188                                           uint64_t usage, bool* outSupported) {
189     return mMapper->isSupported(width, height, format, layerCount, usage, outSupported);
190 }
191 
getBufferId(buffer_handle_t bufferHandle,uint64_t * outBufferId)192 status_t GraphicBufferMapper::getBufferId(buffer_handle_t bufferHandle, uint64_t* outBufferId) {
193     return mMapper->getBufferId(bufferHandle, outBufferId);
194 }
195 
getName(buffer_handle_t bufferHandle,std::string * outName)196 status_t GraphicBufferMapper::getName(buffer_handle_t bufferHandle, std::string* outName) {
197     return mMapper->getName(bufferHandle, outName);
198 }
199 
getWidth(buffer_handle_t bufferHandle,uint64_t * outWidth)200 status_t GraphicBufferMapper::getWidth(buffer_handle_t bufferHandle, uint64_t* outWidth) {
201     return mMapper->getWidth(bufferHandle, outWidth);
202 }
203 
getHeight(buffer_handle_t bufferHandle,uint64_t * outHeight)204 status_t GraphicBufferMapper::getHeight(buffer_handle_t bufferHandle, uint64_t* outHeight) {
205     return mMapper->getHeight(bufferHandle, outHeight);
206 }
207 
getLayerCount(buffer_handle_t bufferHandle,uint64_t * outLayerCount)208 status_t GraphicBufferMapper::getLayerCount(buffer_handle_t bufferHandle, uint64_t* outLayerCount) {
209     return mMapper->getLayerCount(bufferHandle, outLayerCount);
210 }
211 
getPixelFormatRequested(buffer_handle_t bufferHandle,ui::PixelFormat * outPixelFormatRequested)212 status_t GraphicBufferMapper::getPixelFormatRequested(buffer_handle_t bufferHandle,
213                                                       ui::PixelFormat* outPixelFormatRequested) {
214     return mMapper->getPixelFormatRequested(bufferHandle, outPixelFormatRequested);
215 }
216 
getPixelFormatFourCC(buffer_handle_t bufferHandle,uint32_t * outPixelFormatFourCC)217 status_t GraphicBufferMapper::getPixelFormatFourCC(buffer_handle_t bufferHandle,
218                                                    uint32_t* outPixelFormatFourCC) {
219     return mMapper->getPixelFormatFourCC(bufferHandle, outPixelFormatFourCC);
220 }
221 
getPixelFormatModifier(buffer_handle_t bufferHandle,uint64_t * outPixelFormatModifier)222 status_t GraphicBufferMapper::getPixelFormatModifier(buffer_handle_t bufferHandle,
223                                                      uint64_t* outPixelFormatModifier) {
224     return mMapper->getPixelFormatModifier(bufferHandle, outPixelFormatModifier);
225 }
226 
getUsage(buffer_handle_t bufferHandle,uint64_t * outUsage)227 status_t GraphicBufferMapper::getUsage(buffer_handle_t bufferHandle, uint64_t* outUsage) {
228     return mMapper->getUsage(bufferHandle, outUsage);
229 }
230 
getAllocationSize(buffer_handle_t bufferHandle,uint64_t * outAllocationSize)231 status_t GraphicBufferMapper::getAllocationSize(buffer_handle_t bufferHandle,
232                                                 uint64_t* outAllocationSize) {
233     return mMapper->getAllocationSize(bufferHandle, outAllocationSize);
234 }
235 
getProtectedContent(buffer_handle_t bufferHandle,uint64_t * outProtectedContent)236 status_t GraphicBufferMapper::getProtectedContent(buffer_handle_t bufferHandle,
237                                                   uint64_t* outProtectedContent) {
238     return mMapper->getProtectedContent(bufferHandle, outProtectedContent);
239 }
240 
getCompression(buffer_handle_t bufferHandle,aidl::android::hardware::graphics::common::ExtendableType * outCompression)241 status_t GraphicBufferMapper::getCompression(
242         buffer_handle_t bufferHandle,
243         aidl::android::hardware::graphics::common::ExtendableType* outCompression) {
244     return mMapper->getCompression(bufferHandle, outCompression);
245 }
246 
getCompression(buffer_handle_t bufferHandle,ui::Compression * outCompression)247 status_t GraphicBufferMapper::getCompression(buffer_handle_t bufferHandle,
248                                              ui::Compression* outCompression) {
249     return mMapper->getCompression(bufferHandle, outCompression);
250 }
251 
getInterlaced(buffer_handle_t bufferHandle,aidl::android::hardware::graphics::common::ExtendableType * outInterlaced)252 status_t GraphicBufferMapper::getInterlaced(
253         buffer_handle_t bufferHandle,
254         aidl::android::hardware::graphics::common::ExtendableType* outInterlaced) {
255     return mMapper->getInterlaced(bufferHandle, outInterlaced);
256 }
257 
getInterlaced(buffer_handle_t bufferHandle,ui::Interlaced * outInterlaced)258 status_t GraphicBufferMapper::getInterlaced(buffer_handle_t bufferHandle,
259                                             ui::Interlaced* outInterlaced) {
260     return mMapper->getInterlaced(bufferHandle, outInterlaced);
261 }
262 
getChromaSiting(buffer_handle_t bufferHandle,aidl::android::hardware::graphics::common::ExtendableType * outChromaSiting)263 status_t GraphicBufferMapper::getChromaSiting(
264         buffer_handle_t bufferHandle,
265         aidl::android::hardware::graphics::common::ExtendableType* outChromaSiting) {
266     return mMapper->getChromaSiting(bufferHandle, outChromaSiting);
267 }
268 
getChromaSiting(buffer_handle_t bufferHandle,ui::ChromaSiting * outChromaSiting)269 status_t GraphicBufferMapper::getChromaSiting(buffer_handle_t bufferHandle,
270                                               ui::ChromaSiting* outChromaSiting) {
271     return mMapper->getChromaSiting(bufferHandle, outChromaSiting);
272 }
273 
getPlaneLayouts(buffer_handle_t bufferHandle,std::vector<ui::PlaneLayout> * outPlaneLayouts)274 status_t GraphicBufferMapper::getPlaneLayouts(buffer_handle_t bufferHandle,
275                                               std::vector<ui::PlaneLayout>* outPlaneLayouts) {
276     return mMapper->getPlaneLayouts(bufferHandle, outPlaneLayouts);
277 }
278 
getDataspace(buffer_handle_t bufferHandle,ui::Dataspace * outDataspace)279 status_t GraphicBufferMapper::getDataspace(buffer_handle_t bufferHandle,
280                                            ui::Dataspace* outDataspace) {
281     return mMapper->getDataspace(bufferHandle, outDataspace);
282 }
283 
getBlendMode(buffer_handle_t bufferHandle,ui::BlendMode * outBlendMode)284 status_t GraphicBufferMapper::getBlendMode(buffer_handle_t bufferHandle,
285                                            ui::BlendMode* outBlendMode) {
286     return mMapper->getBlendMode(bufferHandle, outBlendMode);
287 }
288 
getSmpte2086(buffer_handle_t bufferHandle,std::optional<ui::Smpte2086> * outSmpte2086)289 status_t GraphicBufferMapper::getSmpte2086(buffer_handle_t bufferHandle,
290                                            std::optional<ui::Smpte2086>* outSmpte2086) {
291     return mMapper->getSmpte2086(bufferHandle, outSmpte2086);
292 }
293 
getCta861_3(buffer_handle_t bufferHandle,std::optional<ui::Cta861_3> * outCta861_3)294 status_t GraphicBufferMapper::getCta861_3(buffer_handle_t bufferHandle,
295                                           std::optional<ui::Cta861_3>* outCta861_3) {
296     return mMapper->getCta861_3(bufferHandle, outCta861_3);
297 }
298 
getSmpte2094_40(buffer_handle_t bufferHandle,std::optional<std::vector<uint8_t>> * outSmpte2094_40)299 status_t GraphicBufferMapper::getSmpte2094_40(
300         buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>>* outSmpte2094_40) {
301     return mMapper->getSmpte2094_40(bufferHandle, outSmpte2094_40);
302 }
303 
getDefaultPixelFormatFourCC(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,uint32_t * outPixelFormatFourCC)304 status_t GraphicBufferMapper::getDefaultPixelFormatFourCC(uint32_t width, uint32_t height,
305                                                           PixelFormat format, uint32_t layerCount,
306                                                           uint64_t usage,
307                                                           uint32_t* outPixelFormatFourCC) {
308     return mMapper->getDefaultPixelFormatFourCC(width, height, format, layerCount, usage,
309                                                 outPixelFormatFourCC);
310 }
311 
getDefaultPixelFormatModifier(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,uint64_t * outPixelFormatModifier)312 status_t GraphicBufferMapper::getDefaultPixelFormatModifier(uint32_t width, uint32_t height,
313                                                             PixelFormat format, uint32_t layerCount,
314                                                             uint64_t usage,
315                                                             uint64_t* outPixelFormatModifier) {
316     return mMapper->getDefaultPixelFormatModifier(width, height, format, layerCount, usage,
317                                                   outPixelFormatModifier);
318 }
319 
getDefaultAllocationSize(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,uint64_t * outAllocationSize)320 status_t GraphicBufferMapper::getDefaultAllocationSize(uint32_t width, uint32_t height,
321                                                        PixelFormat format, uint32_t layerCount,
322                                                        uint64_t usage,
323                                                        uint64_t* outAllocationSize) {
324     return mMapper->getDefaultAllocationSize(width, height, format, layerCount, usage,
325                                              outAllocationSize);
326 }
327 
getDefaultProtectedContent(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,uint64_t * outProtectedContent)328 status_t GraphicBufferMapper::getDefaultProtectedContent(uint32_t width, uint32_t height,
329                                                          PixelFormat format, uint32_t layerCount,
330                                                          uint64_t usage,
331                                                          uint64_t* outProtectedContent) {
332     return mMapper->getDefaultProtectedContent(width, height, format, layerCount, usage,
333                                                outProtectedContent);
334 }
335 
getDefaultCompression(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,aidl::android::hardware::graphics::common::ExtendableType * outCompression)336 status_t GraphicBufferMapper::getDefaultCompression(
337         uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount, uint64_t usage,
338         aidl::android::hardware::graphics::common::ExtendableType* outCompression) {
339     return mMapper->getDefaultCompression(width, height, format, layerCount, usage, outCompression);
340 }
341 
getDefaultCompression(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,ui::Compression * outCompression)342 status_t GraphicBufferMapper::getDefaultCompression(uint32_t width, uint32_t height,
343                                                     PixelFormat format, uint32_t layerCount,
344                                                     uint64_t usage,
345                                                     ui::Compression* outCompression) {
346     return mMapper->getDefaultCompression(width, height, format, layerCount, usage, outCompression);
347 }
348 
getDefaultInterlaced(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,aidl::android::hardware::graphics::common::ExtendableType * outInterlaced)349 status_t GraphicBufferMapper::getDefaultInterlaced(
350         uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount, uint64_t usage,
351         aidl::android::hardware::graphics::common::ExtendableType* outInterlaced) {
352     return mMapper->getDefaultInterlaced(width, height, format, layerCount, usage, outInterlaced);
353 }
354 
getDefaultInterlaced(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,ui::Interlaced * outInterlaced)355 status_t GraphicBufferMapper::getDefaultInterlaced(uint32_t width, uint32_t height,
356                                                    PixelFormat format, uint32_t layerCount,
357                                                    uint64_t usage, ui::Interlaced* outInterlaced) {
358     return mMapper->getDefaultInterlaced(width, height, format, layerCount, usage, outInterlaced);
359 }
360 
getDefaultChromaSiting(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,aidl::android::hardware::graphics::common::ExtendableType * outChromaSiting)361 status_t GraphicBufferMapper::getDefaultChromaSiting(
362         uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount, uint64_t usage,
363         aidl::android::hardware::graphics::common::ExtendableType* outChromaSiting) {
364     return mMapper->getDefaultChromaSiting(width, height, format, layerCount, usage,
365                                            outChromaSiting);
366 }
367 
getDefaultChromaSiting(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,ui::ChromaSiting * outChromaSiting)368 status_t GraphicBufferMapper::getDefaultChromaSiting(uint32_t width, uint32_t height,
369                                                      PixelFormat format, uint32_t layerCount,
370                                                      uint64_t usage,
371                                                      ui::ChromaSiting* outChromaSiting) {
372     return mMapper->getDefaultChromaSiting(width, height, format, layerCount, usage,
373                                            outChromaSiting);
374 }
375 
getDefaultPlaneLayouts(uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,std::vector<ui::PlaneLayout> * outPlaneLayouts)376 status_t GraphicBufferMapper::getDefaultPlaneLayouts(
377         uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount, uint64_t usage,
378         std::vector<ui::PlaneLayout>* outPlaneLayouts) {
379     return mMapper->getDefaultPlaneLayouts(width, height, format, layerCount, usage,
380                                            outPlaneLayouts);
381 }
382 
383 // ---------------------------------------------------------------------------
384 }; // namespace android
385