blob: 7b5a27d9e157abe62a23e1c03b3cc93b8df57d3b [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
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
Mathias Agopian3330b202009-10-05 17:07:12 -070017#define LOG_TAG "GraphicBufferMapper"
Mathias Agopiancf563192012-02-29 20:43:29 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Dan Stoza8deb4da2016-06-01 18:21:44 -070019//#define LOG_NDEBUG 0
Mathias Agopian076b1cc2009-04-10 14:24:30 -070020
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080021#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022
Chia-I Wu5bac7f32017-04-06 12:34:32 -070023#include <grallocusage/GrallocUsageConversion.h>
24
Dan Stozad3182402014-11-17 12:03:59 -080025// 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"
Francis Hart8f396012014-04-01 15:30:53 +030029#include <sync/sync.h>
Dan Stozad3182402014-11-17 12:03:59 -080030#pragma clang diagnostic pop
Francis Hart8f396012014-04-01 15:30:53 +030031
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <utils/Log.h>
Mathias Agopiancf563192012-02-29 20:43:29 -080033#include <utils/Trace.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034
Marissa Walld380e2c2018-12-29 14:17:29 -080035#include <ui/Gralloc.h>
Chia-I Wu5bac7f32017-04-06 12:34:32 -070036#include <ui/Gralloc2.h>
Marissa Wall925bf7f2018-12-29 14:27:11 -080037#include <ui/Gralloc3.h>
Marissa Wall87c8ba72019-06-20 14:20:52 -070038#include <ui/Gralloc4.h>
John Reck0ff95c92022-12-08 11:45:29 -050039#include <ui/Gralloc5.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080040#include <ui/GraphicBuffer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041
Dan Stoza8deb4da2016-06-01 18:21:44 -070042#include <system/graphics.h>
Mathias Agopian8b765b72009-04-10 20:34:46 -070043
John Reck434bc982023-12-19 17:04:07 -050044using unique_fd = ::android::base::unique_fd;
45
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046namespace android {
47// ---------------------------------------------------------------------------
48
John Reck434bc982023-12-19 17:04:07 -050049using LockResult = GraphicBufferMapper::LockResult;
50
Mathias Agopian3330b202009-10-05 17:07:12 -070051ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
Mathias Agopian4243e662009-04-15 18:34:24 -070052
Jesse Halleeb4ac72017-07-06 14:02:29 -070053void GraphicBufferMapper::preloadHal() {
Marissa Walld380e2c2018-12-29 14:17:29 -080054 Gralloc2Mapper::preload();
Marissa Wall925bf7f2018-12-29 14:27:11 -080055 Gralloc3Mapper::preload();
Marissa Wall87c8ba72019-06-20 14:20:52 -070056 Gralloc4Mapper::preload();
John Reck0ff95c92022-12-08 11:45:29 -050057 Gralloc5Mapper::preload();
Jesse Halleeb4ac72017-07-06 14:02:29 -070058}
59
Marissa Wall925bf7f2018-12-29 14:27:11 -080060GraphicBufferMapper::GraphicBufferMapper() {
John Reck0ff95c92022-12-08 11:45:29 -050061 mMapper = std::make_unique<const Gralloc5Mapper>();
62 if (mMapper->isLoaded()) {
63 mMapperVersion = Version::GRALLOC_5;
64 return;
65 }
Marissa Wall87c8ba72019-06-20 14:20:52 -070066 mMapper = std::make_unique<const Gralloc4Mapper>();
67 if (mMapper->isLoaded()) {
68 mMapperVersion = Version::GRALLOC_4;
69 return;
70 }
Marissa Wall925bf7f2018-12-29 14:27:11 -080071 mMapper = std::make_unique<const Gralloc3Mapper>();
Marissa Wall87c8ba72019-06-20 14:20:52 -070072 if (mMapper->isLoaded()) {
Valerie Haud2f4daf2019-02-15 13:49:00 -080073 mMapperVersion = Version::GRALLOC_3;
Marissa Wall87c8ba72019-06-20 14:20:52 -070074 return;
75 }
76 mMapper = std::make_unique<const Gralloc2Mapper>();
77 if (mMapper->isLoaded()) {
78 mMapperVersion = Version::GRALLOC_2;
79 return;
Marissa Wall925bf7f2018-12-29 14:27:11 -080080 }
81
Marissa Wall87c8ba72019-06-20 14:20:52 -070082 LOG_ALWAYS_FATAL("gralloc-mapper is missing");
Marissa Wall925bf7f2018-12-29 14:27:11 -080083}
Dan Stoza8deb4da2016-06-01 18:21:44 -070084
Marissa Wall22b2de12019-12-02 18:11:43 -080085void GraphicBufferMapper::dumpBuffer(buffer_handle_t bufferHandle, std::string& result,
86 bool less) const {
87 result.append(mMapper->dumpBuffer(bufferHandle, less));
88}
89
90void GraphicBufferMapper::dumpBufferToSystemLog(buffer_handle_t bufferHandle, bool less) {
91 std::string s;
92 GraphicBufferMapper::getInstance().dumpBuffer(bufferHandle, s, less);
93 ALOGD("%s", s.c_str());
94}
95
John Reck0ff95c92022-12-08 11:45:29 -050096status_t GraphicBufferMapper::importBuffer(const native_handle_t* rawHandle, uint32_t width,
97 uint32_t height, uint32_t layerCount, PixelFormat format,
98 uint64_t usage, uint32_t stride,
99 buffer_handle_t* outHandle) {
Mathias Agopiancf563192012-02-29 20:43:29 -0800100 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800101
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700102 buffer_handle_t bufferHandle;
John Reck0ff95c92022-12-08 11:45:29 -0500103 status_t error = mMapper->importBuffer(rawHandle, &bufferHandle);
Marissa Wall1e779252018-12-29 12:01:57 -0800104 if (error != NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700105 ALOGW("importBuffer(%p) failed: %d", rawHandle, error);
Marissa Wall1e779252018-12-29 12:01:57 -0800106 return error;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700107 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700108
Marissa Wall1e779252018-12-29 12:01:57 -0800109 error = mMapper->validateBufferSize(bufferHandle, width, height, format, layerCount, usage,
110 stride);
111 if (error != NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700112 ALOGE("validateBufferSize(%p) failed: %d", rawHandle, error);
113 freeBuffer(bufferHandle);
114 return static_cast<status_t>(error);
115 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700116
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700117 *outHandle = bufferHandle;
118
119 return NO_ERROR;
120}
121
John Reck0ff95c92022-12-08 11:45:29 -0500122status_t GraphicBufferMapper::importBufferNoValidate(const native_handle_t* rawHandle,
123 buffer_handle_t* outHandle) {
124 return mMapper->importBuffer(rawHandle, outHandle);
125}
126
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700127void GraphicBufferMapper::getTransportSize(buffer_handle_t handle,
128 uint32_t* outTransportNumFds, uint32_t* outTransportNumInts)
129{
130 mMapper->getTransportSize(handle, outTransportNumFds, outTransportNumInts);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700131}
132
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700133status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134{
Mathias Agopiancf563192012-02-29 20:43:29 -0800135 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800136
Chia-I Wucb8405e2017-04-17 15:20:19 -0700137 mMapper->freeBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800138
Chia-I Wucb8405e2017-04-17 15:20:19 -0700139 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140}
141
John Reck434bc982023-12-19 17:04:07 -0500142ui::Result<LockResult> GraphicBufferMapper::lock(buffer_handle_t handle, int64_t usage,
143 const Rect& bounds, unique_fd&& acquireFence) {
144 ATRACE_CALL();
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700145
John Reck434bc982023-12-19 17:04:07 -0500146 LockResult result;
147 status_t status = mMapper->lock(handle, usage, bounds, acquireFence.release(), &result.address,
148 &result.bytesPerPixel, &result.bytesPerStride);
149 if (status != OK) {
150 return base::unexpected(ui::Error::statusToCode(status));
151 } else {
152 return result;
Dan Stoza8deb4da2016-06-01 18:21:44 -0700153 }
John Reck434bc982023-12-19 17:04:07 -0500154}
155
156ui::Result<android_ycbcr> GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, int64_t usage,
157 const Rect& bounds,
158 base::unique_fd&& acquireFence) {
159 ATRACE_CALL();
160
161 android_ycbcr result = {};
162 status_t status = mMapper->lock(handle, usage, bounds, acquireFence.release(), &result);
163 if (status != OK) {
164 return base::unexpected(ui::Error::statusToCode(status));
165 } else {
166 return result;
167 }
168}
169
170status_t GraphicBufferMapper::unlock(buffer_handle_t handle, base::unique_fd* outFence) {
171 ATRACE_CALL();
172 int fence = mMapper->unlock(handle);
173 if (outFence) {
174 *outFence = unique_fd{fence};
175 } else {
176 sync_wait(fence, -1);
177 close(fence);
178 }
179 return OK;
180}
181
182status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
183 void** vaddr) {
184 auto result = lock(handle, static_cast<int64_t>(usage), bounds);
185 if (!result.has_value()) return result.asStatus();
186 auto val = result.value();
187 *vaddr = val.address;
188 return OK;
189}
190
191status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
192 android_ycbcr* ycbcr) {
193 auto result = lockYCbCr(handle, static_cast<int64_t>(usage), bounds);
194 if (!result.has_value()) return result.asStatus();
195 *ycbcr = result.value();
196 return OK;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700197}
198
Valerie Hau0c9fc362019-01-22 09:17:19 -0800199status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
John Reck434bc982023-12-19 17:04:07 -0500200 void** vaddr, int fenceFd) {
201 auto result = lock(handle, static_cast<int64_t>(usage), bounds, unique_fd{fenceFd});
202 if (!result.has_value()) return result.asStatus();
203 auto val = result.value();
204 *vaddr = val.address;
205 return OK;
Craig Donnere96a3252017-02-02 12:13:34 -0800206}
207
Valerie Hau0c9fc362019-01-22 09:17:19 -0800208status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint64_t producerUsage,
209 uint64_t consumerUsage, const Rect& bounds, void** vaddr,
John Reck434bc982023-12-19 17:04:07 -0500210 int fenceFd) {
Fang Huia3d73332024-07-22 22:11:40 +0800211 return lockAsync(handle,
212 ANDROID_NATIVE_UNSIGNED_CAST(
213 android_convertGralloc1To0Usage(producerUsage, consumerUsage)),
214 bounds, vaddr, fenceFd);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700215}
216
John Reck434bc982023-12-19 17:04:07 -0500217status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle, uint32_t usage,
218 const Rect& bounds, android_ycbcr* ycbcr,
219 int fenceFd) {
220 auto result = lockYCbCr(handle, static_cast<int64_t>(usage), bounds, unique_fd{fenceFd});
221 if (!result.has_value()) return result.asStatus();
222 *ycbcr = result.value();
223 return OK;
Francis Hart8f396012014-04-01 15:30:53 +0300224}
225
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800226status_t GraphicBufferMapper::isSupported(uint32_t width, uint32_t height,
227 android::PixelFormat format, uint32_t layerCount,
228 uint64_t usage, bool* outSupported) {
229 return mMapper->isSupported(width, height, format, layerCount, usage, outSupported);
230}
Marissa Wall22b2de12019-12-02 18:11:43 -0800231
232status_t GraphicBufferMapper::getBufferId(buffer_handle_t bufferHandle, uint64_t* outBufferId) {
233 return mMapper->getBufferId(bufferHandle, outBufferId);
234}
235
236status_t GraphicBufferMapper::getName(buffer_handle_t bufferHandle, std::string* outName) {
237 return mMapper->getName(bufferHandle, outName);
238}
239
240status_t GraphicBufferMapper::getWidth(buffer_handle_t bufferHandle, uint64_t* outWidth) {
241 return mMapper->getWidth(bufferHandle, outWidth);
242}
243
244status_t GraphicBufferMapper::getHeight(buffer_handle_t bufferHandle, uint64_t* outHeight) {
245 return mMapper->getHeight(bufferHandle, outHeight);
246}
247
248status_t GraphicBufferMapper::getLayerCount(buffer_handle_t bufferHandle, uint64_t* outLayerCount) {
249 return mMapper->getLayerCount(bufferHandle, outLayerCount);
250}
251
252status_t GraphicBufferMapper::getPixelFormatRequested(buffer_handle_t bufferHandle,
253 ui::PixelFormat* outPixelFormatRequested) {
254 return mMapper->getPixelFormatRequested(bufferHandle, outPixelFormatRequested);
255}
256
257status_t GraphicBufferMapper::getPixelFormatFourCC(buffer_handle_t bufferHandle,
258 uint32_t* outPixelFormatFourCC) {
259 return mMapper->getPixelFormatFourCC(bufferHandle, outPixelFormatFourCC);
260}
261
262status_t GraphicBufferMapper::getPixelFormatModifier(buffer_handle_t bufferHandle,
263 uint64_t* outPixelFormatModifier) {
264 return mMapper->getPixelFormatModifier(bufferHandle, outPixelFormatModifier);
265}
266
267status_t GraphicBufferMapper::getUsage(buffer_handle_t bufferHandle, uint64_t* outUsage) {
268 return mMapper->getUsage(bufferHandle, outUsage);
269}
270
271status_t GraphicBufferMapper::getAllocationSize(buffer_handle_t bufferHandle,
272 uint64_t* outAllocationSize) {
273 return mMapper->getAllocationSize(bufferHandle, outAllocationSize);
274}
275
276status_t GraphicBufferMapper::getProtectedContent(buffer_handle_t bufferHandle,
277 uint64_t* outProtectedContent) {
278 return mMapper->getProtectedContent(bufferHandle, outProtectedContent);
279}
280
281status_t GraphicBufferMapper::getCompression(
282 buffer_handle_t bufferHandle,
283 aidl::android::hardware::graphics::common::ExtendableType* outCompression) {
284 return mMapper->getCompression(bufferHandle, outCompression);
285}
286
287status_t GraphicBufferMapper::getCompression(buffer_handle_t bufferHandle,
288 ui::Compression* outCompression) {
289 return mMapper->getCompression(bufferHandle, outCompression);
290}
291
292status_t GraphicBufferMapper::getInterlaced(
293 buffer_handle_t bufferHandle,
294 aidl::android::hardware::graphics::common::ExtendableType* outInterlaced) {
295 return mMapper->getInterlaced(bufferHandle, outInterlaced);
296}
297
298status_t GraphicBufferMapper::getInterlaced(buffer_handle_t bufferHandle,
299 ui::Interlaced* outInterlaced) {
300 return mMapper->getInterlaced(bufferHandle, outInterlaced);
301}
302
303status_t GraphicBufferMapper::getChromaSiting(
304 buffer_handle_t bufferHandle,
305 aidl::android::hardware::graphics::common::ExtendableType* outChromaSiting) {
306 return mMapper->getChromaSiting(bufferHandle, outChromaSiting);
307}
308
309status_t GraphicBufferMapper::getChromaSiting(buffer_handle_t bufferHandle,
310 ui::ChromaSiting* outChromaSiting) {
311 return mMapper->getChromaSiting(bufferHandle, outChromaSiting);
312}
313
314status_t GraphicBufferMapper::getPlaneLayouts(buffer_handle_t bufferHandle,
315 std::vector<ui::PlaneLayout>* outPlaneLayouts) {
316 return mMapper->getPlaneLayouts(bufferHandle, outPlaneLayouts);
317}
318
John Reck434bc982023-12-19 17:04:07 -0500319ui::Result<std::vector<ui::PlaneLayout>> GraphicBufferMapper::getPlaneLayouts(
320 buffer_handle_t bufferHandle) {
321 std::vector<ui::PlaneLayout> temp;
322 status_t status = mMapper->getPlaneLayouts(bufferHandle, &temp);
323 if (status == OK) {
324 return std::move(temp);
325 } else {
326 return base::unexpected(ui::Error::statusToCode(status));
327 }
328}
329
Marissa Wall22b2de12019-12-02 18:11:43 -0800330status_t GraphicBufferMapper::getDataspace(buffer_handle_t bufferHandle,
331 ui::Dataspace* outDataspace) {
332 return mMapper->getDataspace(bufferHandle, outDataspace);
333}
334
Alec Mouri9c604e32022-03-18 22:47:44 +0000335status_t GraphicBufferMapper::setDataspace(buffer_handle_t bufferHandle, ui::Dataspace dataspace) {
336 return mMapper->setDataspace(bufferHandle, dataspace);
337}
338
Marissa Wall22b2de12019-12-02 18:11:43 -0800339status_t GraphicBufferMapper::getBlendMode(buffer_handle_t bufferHandle,
340 ui::BlendMode* outBlendMode) {
341 return mMapper->getBlendMode(bufferHandle, outBlendMode);
342}
343
Marissa Wallef785e12019-12-12 14:26:59 -0800344status_t GraphicBufferMapper::getSmpte2086(buffer_handle_t bufferHandle,
345 std::optional<ui::Smpte2086>* outSmpte2086) {
346 return mMapper->getSmpte2086(bufferHandle, outSmpte2086);
347}
348
Alec Mouri9c604e32022-03-18 22:47:44 +0000349status_t GraphicBufferMapper::setSmpte2086(buffer_handle_t bufferHandle,
350 std::optional<ui::Smpte2086> smpte2086) {
351 return mMapper->setSmpte2086(bufferHandle, smpte2086);
352}
353
Marissa Wallef785e12019-12-12 14:26:59 -0800354status_t GraphicBufferMapper::getCta861_3(buffer_handle_t bufferHandle,
355 std::optional<ui::Cta861_3>* outCta861_3) {
356 return mMapper->getCta861_3(bufferHandle, outCta861_3);
357}
358
Alec Mouri9c604e32022-03-18 22:47:44 +0000359status_t GraphicBufferMapper::setCta861_3(buffer_handle_t bufferHandle,
360 std::optional<ui::Cta861_3> cta861_3) {
361 return mMapper->setCta861_3(bufferHandle, cta861_3);
362}
363
Marissa Wallef785e12019-12-12 14:26:59 -0800364status_t GraphicBufferMapper::getSmpte2094_40(
365 buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>>* outSmpte2094_40) {
366 return mMapper->getSmpte2094_40(bufferHandle, outSmpte2094_40);
367}
368
Alec Mouri9c604e32022-03-18 22:47:44 +0000369status_t GraphicBufferMapper::setSmpte2094_40(buffer_handle_t bufferHandle,
370 std::optional<std::vector<uint8_t>> smpte2094_40) {
371 return mMapper->setSmpte2094_40(bufferHandle, smpte2094_40);
372}
373
Alec Mouri332765e2021-10-06 16:38:12 -0700374status_t GraphicBufferMapper::getSmpte2094_10(
375 buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>>* outSmpte2094_10) {
376 return mMapper->getSmpte2094_10(bufferHandle, outSmpte2094_10);
377}
378
Alec Mouri9c604e32022-03-18 22:47:44 +0000379status_t GraphicBufferMapper::setSmpte2094_10(buffer_handle_t bufferHandle,
380 std::optional<std::vector<uint8_t>> smpte2094_10) {
381 return mMapper->setSmpte2094_10(bufferHandle, smpte2094_10);
382}
383
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700384// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700385}; // namespace android