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/Gralloc5.h>
40 #include <ui/GraphicBuffer.h>
41
42 #include <system/graphics.h>
43
44 namespace android {
45 // ---------------------------------------------------------------------------
46
ANDROID_SINGLETON_STATIC_INSTANCE(GraphicBufferMapper)47 ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
48
49 void GraphicBufferMapper::preloadHal() {
50 Gralloc2Mapper::preload();
51 Gralloc3Mapper::preload();
52 Gralloc4Mapper::preload();
53 Gralloc5Mapper::preload();
54 }
55
GraphicBufferMapper()56 GraphicBufferMapper::GraphicBufferMapper() {
57 mMapper = std::make_unique<const Gralloc5Mapper>();
58 if (mMapper->isLoaded()) {
59 mMapperVersion = Version::GRALLOC_5;
60 return;
61 }
62 mMapper = std::make_unique<const Gralloc4Mapper>();
63 if (mMapper->isLoaded()) {
64 mMapperVersion = Version::GRALLOC_4;
65 return;
66 }
67 mMapper = std::make_unique<const Gralloc3Mapper>();
68 if (mMapper->isLoaded()) {
69 mMapperVersion = Version::GRALLOC_3;
70 return;
71 }
72 mMapper = std::make_unique<const Gralloc2Mapper>();
73 if (mMapper->isLoaded()) {
74 mMapperVersion = Version::GRALLOC_2;
75 return;
76 }
77
78 LOG_ALWAYS_FATAL("gralloc-mapper is missing");
79 }
80
dumpBuffer(buffer_handle_t bufferHandle,std::string & result,bool less) const81 void GraphicBufferMapper::dumpBuffer(buffer_handle_t bufferHandle, std::string& result,
82 bool less) const {
83 result.append(mMapper->dumpBuffer(bufferHandle, less));
84 }
85
dumpBufferToSystemLog(buffer_handle_t bufferHandle,bool less)86 void GraphicBufferMapper::dumpBufferToSystemLog(buffer_handle_t bufferHandle, bool less) {
87 std::string s;
88 GraphicBufferMapper::getInstance().dumpBuffer(bufferHandle, s, less);
89 ALOGD("%s", s.c_str());
90 }
91
importBuffer(const native_handle_t * rawHandle,uint32_t width,uint32_t height,uint32_t layerCount,PixelFormat format,uint64_t usage,uint32_t stride,buffer_handle_t * outHandle)92 status_t GraphicBufferMapper::importBuffer(const native_handle_t* rawHandle, uint32_t width,
93 uint32_t height, uint32_t layerCount, PixelFormat format,
94 uint64_t usage, uint32_t stride,
95 buffer_handle_t* outHandle) {
96 ATRACE_CALL();
97
98 buffer_handle_t bufferHandle;
99 status_t error = mMapper->importBuffer(rawHandle, &bufferHandle);
100 if (error != NO_ERROR) {
101 ALOGW("importBuffer(%p) failed: %d", rawHandle, error);
102 return error;
103 }
104
105 error = mMapper->validateBufferSize(bufferHandle, width, height, format, layerCount, usage,
106 stride);
107 if (error != NO_ERROR) {
108 ALOGE("validateBufferSize(%p) failed: %d", rawHandle, error);
109 freeBuffer(bufferHandle);
110 return static_cast<status_t>(error);
111 }
112
113 *outHandle = bufferHandle;
114
115 return NO_ERROR;
116 }
117
importBufferNoValidate(const native_handle_t * rawHandle,buffer_handle_t * outHandle)118 status_t GraphicBufferMapper::importBufferNoValidate(const native_handle_t* rawHandle,
119 buffer_handle_t* outHandle) {
120 return mMapper->importBuffer(rawHandle, outHandle);
121 }
122
getTransportSize(buffer_handle_t handle,uint32_t * outTransportNumFds,uint32_t * outTransportNumInts)123 void GraphicBufferMapper::getTransportSize(buffer_handle_t handle,
124 uint32_t* outTransportNumFds, uint32_t* outTransportNumInts)
125 {
126 mMapper->getTransportSize(handle, outTransportNumFds, outTransportNumInts);
127 }
128
freeBuffer(buffer_handle_t handle)129 status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
130 {
131 ATRACE_CALL();
132
133 mMapper->freeBuffer(handle);
134
135 return NO_ERROR;
136 }
137
lock(buffer_handle_t handle,uint32_t usage,const Rect & bounds,void ** vaddr,int32_t * outBytesPerPixel,int32_t * outBytesPerStride)138 status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
139 void** vaddr, int32_t* outBytesPerPixel,
140 int32_t* outBytesPerStride) {
141 return lockAsync(handle, usage, bounds, vaddr, -1, outBytesPerPixel, outBytesPerStride);
142 }
143
lockYCbCr(buffer_handle_t handle,uint32_t usage,const Rect & bounds,android_ycbcr * ycbcr)144 status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
145 const Rect& bounds, android_ycbcr *ycbcr)
146 {
147 return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
148 }
149
unlock(buffer_handle_t handle)150 status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
151 {
152 int32_t fenceFd = -1;
153 status_t error = unlockAsync(handle, &fenceFd);
154 if (error == NO_ERROR && fenceFd >= 0) {
155 sync_wait(fenceFd, -1);
156 close(fenceFd);
157 }
158 return error;
159 }
160
lockAsync(buffer_handle_t handle,uint32_t usage,const Rect & bounds,void ** vaddr,int fenceFd,int32_t * outBytesPerPixel,int32_t * outBytesPerStride)161 status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
162 void** vaddr, int fenceFd, int32_t* outBytesPerPixel,
163 int32_t* outBytesPerStride) {
164 return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd, outBytesPerPixel,
165 outBytesPerStride);
166 }
167
lockAsync(buffer_handle_t handle,uint64_t producerUsage,uint64_t consumerUsage,const Rect & bounds,void ** vaddr,int fenceFd,int32_t * outBytesPerPixel,int32_t * outBytesPerStride)168 status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint64_t producerUsage,
169 uint64_t consumerUsage, const Rect& bounds, void** vaddr,
170 int fenceFd, int32_t* outBytesPerPixel,
171 int32_t* outBytesPerStride) {
172 ATRACE_CALL();
173
174 const uint64_t usage = static_cast<uint64_t>(
175 android_convertGralloc1To0Usage(producerUsage, consumerUsage));
176 return mMapper->lock(handle, usage, bounds, fenceFd, vaddr, outBytesPerPixel,
177 outBytesPerStride);
178 }
179
lockAsyncYCbCr(buffer_handle_t handle,uint32_t usage,const Rect & bounds,android_ycbcr * ycbcr,int fenceFd)180 status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
181 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
182 {
183 ATRACE_CALL();
184
185 return mMapper->lock(handle, usage, bounds, fenceFd, ycbcr);
186 }
187
unlockAsync(buffer_handle_t handle,int * fenceFd)188 status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
189 {
190 ATRACE_CALL();
191
192 *fenceFd = mMapper->unlock(handle);
193
194 return NO_ERROR;
195 }
196
isSupported(uint32_t width,uint32_t height,android::PixelFormat format,uint32_t layerCount,uint64_t usage,bool * outSupported)197 status_t GraphicBufferMapper::isSupported(uint32_t width, uint32_t height,
198 android::PixelFormat format, uint32_t layerCount,
199 uint64_t usage, bool* outSupported) {
200 return mMapper->isSupported(width, height, format, layerCount, usage, outSupported);
201 }
202
getBufferId(buffer_handle_t bufferHandle,uint64_t * outBufferId)203 status_t GraphicBufferMapper::getBufferId(buffer_handle_t bufferHandle, uint64_t* outBufferId) {
204 return mMapper->getBufferId(bufferHandle, outBufferId);
205 }
206
getName(buffer_handle_t bufferHandle,std::string * outName)207 status_t GraphicBufferMapper::getName(buffer_handle_t bufferHandle, std::string* outName) {
208 return mMapper->getName(bufferHandle, outName);
209 }
210
getWidth(buffer_handle_t bufferHandle,uint64_t * outWidth)211 status_t GraphicBufferMapper::getWidth(buffer_handle_t bufferHandle, uint64_t* outWidth) {
212 return mMapper->getWidth(bufferHandle, outWidth);
213 }
214
getHeight(buffer_handle_t bufferHandle,uint64_t * outHeight)215 status_t GraphicBufferMapper::getHeight(buffer_handle_t bufferHandle, uint64_t* outHeight) {
216 return mMapper->getHeight(bufferHandle, outHeight);
217 }
218
getLayerCount(buffer_handle_t bufferHandle,uint64_t * outLayerCount)219 status_t GraphicBufferMapper::getLayerCount(buffer_handle_t bufferHandle, uint64_t* outLayerCount) {
220 return mMapper->getLayerCount(bufferHandle, outLayerCount);
221 }
222
getPixelFormatRequested(buffer_handle_t bufferHandle,ui::PixelFormat * outPixelFormatRequested)223 status_t GraphicBufferMapper::getPixelFormatRequested(buffer_handle_t bufferHandle,
224 ui::PixelFormat* outPixelFormatRequested) {
225 return mMapper->getPixelFormatRequested(bufferHandle, outPixelFormatRequested);
226 }
227
getPixelFormatFourCC(buffer_handle_t bufferHandle,uint32_t * outPixelFormatFourCC)228 status_t GraphicBufferMapper::getPixelFormatFourCC(buffer_handle_t bufferHandle,
229 uint32_t* outPixelFormatFourCC) {
230 return mMapper->getPixelFormatFourCC(bufferHandle, outPixelFormatFourCC);
231 }
232
getPixelFormatModifier(buffer_handle_t bufferHandle,uint64_t * outPixelFormatModifier)233 status_t GraphicBufferMapper::getPixelFormatModifier(buffer_handle_t bufferHandle,
234 uint64_t* outPixelFormatModifier) {
235 return mMapper->getPixelFormatModifier(bufferHandle, outPixelFormatModifier);
236 }
237
getUsage(buffer_handle_t bufferHandle,uint64_t * outUsage)238 status_t GraphicBufferMapper::getUsage(buffer_handle_t bufferHandle, uint64_t* outUsage) {
239 return mMapper->getUsage(bufferHandle, outUsage);
240 }
241
getAllocationSize(buffer_handle_t bufferHandle,uint64_t * outAllocationSize)242 status_t GraphicBufferMapper::getAllocationSize(buffer_handle_t bufferHandle,
243 uint64_t* outAllocationSize) {
244 return mMapper->getAllocationSize(bufferHandle, outAllocationSize);
245 }
246
getProtectedContent(buffer_handle_t bufferHandle,uint64_t * outProtectedContent)247 status_t GraphicBufferMapper::getProtectedContent(buffer_handle_t bufferHandle,
248 uint64_t* outProtectedContent) {
249 return mMapper->getProtectedContent(bufferHandle, outProtectedContent);
250 }
251
getCompression(buffer_handle_t bufferHandle,aidl::android::hardware::graphics::common::ExtendableType * outCompression)252 status_t GraphicBufferMapper::getCompression(
253 buffer_handle_t bufferHandle,
254 aidl::android::hardware::graphics::common::ExtendableType* outCompression) {
255 return mMapper->getCompression(bufferHandle, outCompression);
256 }
257
getCompression(buffer_handle_t bufferHandle,ui::Compression * outCompression)258 status_t GraphicBufferMapper::getCompression(buffer_handle_t bufferHandle,
259 ui::Compression* outCompression) {
260 return mMapper->getCompression(bufferHandle, outCompression);
261 }
262
getInterlaced(buffer_handle_t bufferHandle,aidl::android::hardware::graphics::common::ExtendableType * outInterlaced)263 status_t GraphicBufferMapper::getInterlaced(
264 buffer_handle_t bufferHandle,
265 aidl::android::hardware::graphics::common::ExtendableType* outInterlaced) {
266 return mMapper->getInterlaced(bufferHandle, outInterlaced);
267 }
268
getInterlaced(buffer_handle_t bufferHandle,ui::Interlaced * outInterlaced)269 status_t GraphicBufferMapper::getInterlaced(buffer_handle_t bufferHandle,
270 ui::Interlaced* outInterlaced) {
271 return mMapper->getInterlaced(bufferHandle, outInterlaced);
272 }
273
getChromaSiting(buffer_handle_t bufferHandle,aidl::android::hardware::graphics::common::ExtendableType * outChromaSiting)274 status_t GraphicBufferMapper::getChromaSiting(
275 buffer_handle_t bufferHandle,
276 aidl::android::hardware::graphics::common::ExtendableType* outChromaSiting) {
277 return mMapper->getChromaSiting(bufferHandle, outChromaSiting);
278 }
279
getChromaSiting(buffer_handle_t bufferHandle,ui::ChromaSiting * outChromaSiting)280 status_t GraphicBufferMapper::getChromaSiting(buffer_handle_t bufferHandle,
281 ui::ChromaSiting* outChromaSiting) {
282 return mMapper->getChromaSiting(bufferHandle, outChromaSiting);
283 }
284
getPlaneLayouts(buffer_handle_t bufferHandle,std::vector<ui::PlaneLayout> * outPlaneLayouts)285 status_t GraphicBufferMapper::getPlaneLayouts(buffer_handle_t bufferHandle,
286 std::vector<ui::PlaneLayout>* outPlaneLayouts) {
287 return mMapper->getPlaneLayouts(bufferHandle, outPlaneLayouts);
288 }
289
getDataspace(buffer_handle_t bufferHandle,ui::Dataspace * outDataspace)290 status_t GraphicBufferMapper::getDataspace(buffer_handle_t bufferHandle,
291 ui::Dataspace* outDataspace) {
292 return mMapper->getDataspace(bufferHandle, outDataspace);
293 }
294
setDataspace(buffer_handle_t bufferHandle,ui::Dataspace dataspace)295 status_t GraphicBufferMapper::setDataspace(buffer_handle_t bufferHandle, ui::Dataspace dataspace) {
296 return mMapper->setDataspace(bufferHandle, dataspace);
297 }
298
getBlendMode(buffer_handle_t bufferHandle,ui::BlendMode * outBlendMode)299 status_t GraphicBufferMapper::getBlendMode(buffer_handle_t bufferHandle,
300 ui::BlendMode* outBlendMode) {
301 return mMapper->getBlendMode(bufferHandle, outBlendMode);
302 }
303
getSmpte2086(buffer_handle_t bufferHandle,std::optional<ui::Smpte2086> * outSmpte2086)304 status_t GraphicBufferMapper::getSmpte2086(buffer_handle_t bufferHandle,
305 std::optional<ui::Smpte2086>* outSmpte2086) {
306 return mMapper->getSmpte2086(bufferHandle, outSmpte2086);
307 }
308
setSmpte2086(buffer_handle_t bufferHandle,std::optional<ui::Smpte2086> smpte2086)309 status_t GraphicBufferMapper::setSmpte2086(buffer_handle_t bufferHandle,
310 std::optional<ui::Smpte2086> smpte2086) {
311 return mMapper->setSmpte2086(bufferHandle, smpte2086);
312 }
313
getCta861_3(buffer_handle_t bufferHandle,std::optional<ui::Cta861_3> * outCta861_3)314 status_t GraphicBufferMapper::getCta861_3(buffer_handle_t bufferHandle,
315 std::optional<ui::Cta861_3>* outCta861_3) {
316 return mMapper->getCta861_3(bufferHandle, outCta861_3);
317 }
318
setCta861_3(buffer_handle_t bufferHandle,std::optional<ui::Cta861_3> cta861_3)319 status_t GraphicBufferMapper::setCta861_3(buffer_handle_t bufferHandle,
320 std::optional<ui::Cta861_3> cta861_3) {
321 return mMapper->setCta861_3(bufferHandle, cta861_3);
322 }
323
getSmpte2094_40(buffer_handle_t bufferHandle,std::optional<std::vector<uint8_t>> * outSmpte2094_40)324 status_t GraphicBufferMapper::getSmpte2094_40(
325 buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>>* outSmpte2094_40) {
326 return mMapper->getSmpte2094_40(bufferHandle, outSmpte2094_40);
327 }
328
setSmpte2094_40(buffer_handle_t bufferHandle,std::optional<std::vector<uint8_t>> smpte2094_40)329 status_t GraphicBufferMapper::setSmpte2094_40(buffer_handle_t bufferHandle,
330 std::optional<std::vector<uint8_t>> smpte2094_40) {
331 return mMapper->setSmpte2094_40(bufferHandle, smpte2094_40);
332 }
333
getSmpte2094_10(buffer_handle_t bufferHandle,std::optional<std::vector<uint8_t>> * outSmpte2094_10)334 status_t GraphicBufferMapper::getSmpte2094_10(
335 buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>>* outSmpte2094_10) {
336 return mMapper->getSmpte2094_10(bufferHandle, outSmpte2094_10);
337 }
338
setSmpte2094_10(buffer_handle_t bufferHandle,std::optional<std::vector<uint8_t>> smpte2094_10)339 status_t GraphicBufferMapper::setSmpte2094_10(buffer_handle_t bufferHandle,
340 std::optional<std::vector<uint8_t>> smpte2094_10) {
341 return mMapper->setSmpte2094_10(bufferHandle, smpte2094_10);
342 }
343
344 // ---------------------------------------------------------------------------
345 }; // namespace android
346