blob: f7c94005f1dceebcc93042ac1eb6f93d7cc18b21 [file] [log] [blame]
Mathias Agopian3330b202009-10-05 17:07:12 -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 Agopian98e71dd2010-02-11 17:30:52 -080017#define LOG_TAG "GraphicBuffer"
Alec Mouri6338c9d2019-02-07 16:57:51 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Mathias Agopian98e71dd2010-02-11 17:30:52 -080019
Mathias Agopian3330b202009-10-05 17:07:12 -070020#include <ui/GraphicBuffer.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080021
22#include <cutils/atomic.h>
23
Jesse Hall79927812017-03-23 11:03:23 -070024#include <grallocusage/GrallocUsageConversion.h>
John Reck434bc982023-12-19 17:04:07 -050025#include <sync/sync.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070026#include <ui/GraphicBufferAllocator.h>
27#include <ui/GraphicBufferMapper.h>
Alec Mouri6338c9d2019-02-07 16:57:51 -080028#include <utils/Trace.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070029
Alec Mouridf868ba2025-02-25 16:08:19 -080030#include <string>
31
Mathias Agopian3330b202009-10-05 17:07:12 -070032namespace android {
33
34// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070035// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070036// ===========================================================================
37
Dan Stozab1363d32014-03-28 15:10:52 -070038static uint64_t getUniqueId() {
39 static volatile int32_t nextId = 0;
40 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
41 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
42 return id;
43}
44
John Reck434bc982023-12-19 17:04:07 -050045static void resolveLegacyByteLayoutFromPlaneLayout(const std::vector<ui::PlaneLayout>& planeLayouts,
46 int32_t* outBytesPerPixel,
47 int32_t* outBytesPerStride) {
48 if (planeLayouts.empty()) return;
49 if (outBytesPerPixel) {
50 int32_t bitsPerPixel = planeLayouts.front().sampleIncrementInBits;
51 for (const auto& planeLayout : planeLayouts) {
52 if (bitsPerPixel != planeLayout.sampleIncrementInBits) {
53 bitsPerPixel = -1;
54 }
55 }
56 if (bitsPerPixel >= 0 && bitsPerPixel % 8 == 0) {
57 *outBytesPerPixel = bitsPerPixel / 8;
58 } else {
59 *outBytesPerPixel = -1;
60 }
61 }
62 if (outBytesPerStride) {
63 int32_t bytesPerStride = planeLayouts.front().strideInBytes;
64 for (const auto& planeLayout : planeLayouts) {
65 if (bytesPerStride != planeLayout.strideInBytes) {
66 bytesPerStride = -1;
67 }
68 }
69 if (bytesPerStride >= 0) {
70 *outBytesPerStride = bytesPerStride;
71 } else {
72 *outBytesPerStride = -1;
73 }
74 }
75}
76
Mathias Agopianf543e5a2017-04-03 17:16:41 -070077sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
78 return static_cast<GraphicBuffer *>(anwb);
79}
80
Pawin Vongmasae672cd02019-02-14 16:01:29 -080081GraphicBuffer* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer* buffer) {
82 return reinterpret_cast<GraphicBuffer*>(buffer);
83}
84
85GraphicBuffer const* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer const* buffer) {
86 return reinterpret_cast<GraphicBuffer const*>(buffer);
87}
88
89AHardwareBuffer* GraphicBuffer::toAHardwareBuffer() {
90 return reinterpret_cast<AHardwareBuffer*>(this);
91}
92
93AHardwareBuffer const* GraphicBuffer::toAHardwareBuffer() const {
94 return reinterpret_cast<AHardwareBuffer const*>(this);
95}
96
Mathias Agopian3330b202009-10-05 17:07:12 -070097GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070098 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070099 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -0700100{
Dan Stoza01049c82014-11-11 10:32:31 -0800101 width =
102 height =
103 stride =
104 format =
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700105 usage_deprecated = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700106 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -0800107 layerCount = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700108 handle = nullptr;
Alec Mouridf868ba2025-02-25 16:08:19 -0800109 mDependencyMonitor.setToken(std::to_string(mId));
Mathias Agopian3330b202009-10-05 17:07:12 -0700110}
111
Chia-I Wub42f1712017-03-21 13:15:39 -0700112// deprecated
Dan Stozad3182402014-11-17 12:03:59 -0800113GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -0700114 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700115 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700116{
Mathias Agopian3330b202009-10-05 17:07:12 -0700117}
118
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800119GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
120 uint32_t inLayerCount, uint64_t inUsage, std::string requestorName)
121 : GraphicBuffer() {
122 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage,
123 std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700124}
125
Chia-I Wub42f1712017-03-21 13:15:39 -0700126// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -0700127GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
128 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
129 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -0700130 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
Chris Forbes82c04982017-04-19 14:29:54 -0700131 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
Chia-I Wub42f1712017-03-21 13:15:39 -0700132 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700133{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700134}
135
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800136GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method,
137 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
138 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride)
139 : GraphicBuffer() {
140 mInitCheck = initWithHandle(inHandle, method, inWidth, inHeight, inFormat, inLayerCount,
141 inUsage, inStride);
Chia-I Wub42f1712017-03-21 13:15:39 -0700142}
143
John Reckd727e9c2023-12-08 11:30:37 -0500144GraphicBuffer::GraphicBuffer(const GraphicBufferAllocator::AllocationRequest& request)
145 : GraphicBuffer() {
146 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
147 auto result = allocator.allocate(request);
148 mInitCheck = result.status;
149 if (result.status == NO_ERROR) {
150 handle = result.handle;
151 stride = result.stride;
152
153 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
154
155 width = static_cast<int>(request.width);
156 height = static_cast<int>(request.height);
157 format = request.format;
158 layerCount = request.layerCount;
159 usage = request.usage;
160 usage_deprecated = int(usage);
Alec Mouridf868ba2025-02-25 16:08:19 -0800161 std::string name = request.requestorName;
162 mDependencyMonitor.setToken(name.append(":").append(std::to_string(mId)));
John Reckd727e9c2023-12-08 11:30:37 -0500163 }
164}
165
Mathias Agopian3330b202009-10-05 17:07:12 -0700166GraphicBuffer::~GraphicBuffer()
167{
Alec Mouri6338c9d2019-02-07 16:57:51 -0800168 ATRACE_CALL();
Mathias Agopian3330b202009-10-05 17:07:12 -0700169 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800170 free_handle();
171 }
Marissa Wall78b72202019-03-15 14:58:34 -0700172 for (auto& [callback, context] : mDeathCallbacks) {
173 callback(context, mId);
174 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800175}
176
177void GraphicBuffer::free_handle()
178{
179 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700180 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800181 } else if (mOwner == ownData) {
182 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
183 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700184 }
Yi Kong48d76082019-03-24 02:01:06 -0700185 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700186}
187
188status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800189 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700190}
191
Mathias Agopian678bdd62010-12-03 17:33:09 -0800192void GraphicBuffer::dumpAllocationsToSystemLog()
193{
194 GraphicBufferAllocator::dumpToSystemLog();
195}
196
Iliyan Malchev697526b2011-05-01 11:33:26 -0700197ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700198{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700199 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700200 const_cast<GraphicBuffer*>(this));
201}
202
John Reckd727e9c2023-12-08 11:30:37 -0500203status_t GraphicBuffer::getDataspace(ui::Dataspace* outDataspace) const {
204 return mBufferMapper.getDataspace(handle, outDataspace);
205}
206
Dan Stozad3182402014-11-17 12:03:59 -0800207status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700208 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700209{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700210 if (mOwner != ownData)
211 return INVALID_OPERATION;
212
Dan Stozad3182402014-11-17 12:03:59 -0800213 if (handle &&
214 static_cast<int>(inWidth) == width &&
215 static_cast<int>(inHeight) == height &&
216 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700217 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700218 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700219 return NO_ERROR;
220
Mathias Agopian3330b202009-10-05 17:07:12 -0700221 if (handle) {
222 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
223 allocator.free(handle);
Yi Kong48d76082019-03-24 02:01:06 -0700224 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700225 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700226 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700227}
228
Dan Stoza9de72932015-04-16 17:28:43 -0700229bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700230 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700231{
232 if (static_cast<int>(inWidth) != width) return true;
233 if (static_cast<int>(inHeight) != height) return true;
234 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700235 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700236 if ((usage & inUsage) != inUsage) return true;
Peiyong Lin5d2018a2019-03-26 15:07:54 -0700237 if ((usage & USAGE_PROTECTED) != (inUsage & USAGE_PROTECTED)) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700238 return false;
239}
240
Chia-I Wub42f1712017-03-21 13:15:39 -0700241status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700242 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
243 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700244{
Mathias Agopian3330b202009-10-05 17:07:12 -0700245 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800246 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700247 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700248 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800249 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700250 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700251 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
252
Dan Stozad3182402014-11-17 12:03:59 -0800253 width = static_cast<int>(inWidth);
254 height = static_cast<int>(inHeight);
255 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700256 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700257 usage = inUsage;
258 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800259 stride = static_cast<int>(outStride);
Alec Mouridf868ba2025-02-25 16:08:19 -0800260 mDependencyMonitor.setToken(requestorName.append(":").append(std::to_string(mId)));
Mathias Agopian3330b202009-10-05 17:07:12 -0700261 }
262 return err;
263}
264
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800265status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
266 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
267 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) {
268 ANativeWindowBuffer::width = static_cast<int>(inWidth);
269 ANativeWindowBuffer::height = static_cast<int>(inHeight);
270 ANativeWindowBuffer::stride = static_cast<int>(inStride);
271 ANativeWindowBuffer::format = inFormat;
272 ANativeWindowBuffer::usage = inUsage;
273 ANativeWindowBuffer::usage_deprecated = int(inUsage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700274
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800275 ANativeWindowBuffer::layerCount = inLayerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700276
277 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
278
Chia-I Wucb8405e2017-04-17 15:20:19 -0700279 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
280 buffer_handle_t importedHandle;
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800281 status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount,
282 inFormat, inUsage, inStride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700283 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700284 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700285
286 return err;
287 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700288
289 if (method == TAKE_UNREGISTERED_HANDLE) {
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800290 native_handle_close(inHandle);
291 native_handle_delete(const_cast<native_handle_t*>(inHandle));
Chia-I Wucb8405e2017-04-17 15:20:19 -0700292 }
293
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800294 inHandle = importedHandle;
295 mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700296 }
297
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800298 ANativeWindowBuffer::handle = inHandle;
Chia-I Wucb8405e2017-04-17 15:20:19 -0700299
Chia-I Wub42f1712017-03-21 13:15:39 -0700300 return NO_ERROR;
301}
302
Valerie Hau250c6542019-01-31 14:23:43 -0800303status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel,
304 int32_t* outBytesPerStride) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700305 const Rect lockBounds(width, height);
Valerie Hau250c6542019-01-31 14:23:43 -0800306 status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700307 return res;
308}
309
Valerie Hau250c6542019-01-31 14:23:43 -0800310status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr,
311 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800312 if (rect.left < 0 || rect.right > width ||
313 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000314 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800315 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800316 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700317 return BAD_VALUE;
318 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800319
John Reck434bc982023-12-19 17:04:07 -0500320 return lockAsync(inUsage, rect, vaddr, -1, outBytesPerPixel, outBytesPerStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700321}
322
Dan Stozad3182402014-11-17 12:03:59 -0800323status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700324{
325 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800326 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700327 return res;
328}
329
Dan Stozad3182402014-11-17 12:03:59 -0800330status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
331 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700332{
Dan Stozad3182402014-11-17 12:03:59 -0800333 if (rect.left < 0 || rect.right > width ||
334 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700335 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
336 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800337 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700338 return BAD_VALUE;
339 }
John Reck434bc982023-12-19 17:04:07 -0500340 return lockAsyncYCbCr(inUsage, rect, ycbcr, -1);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700341}
342
Mathias Agopian3330b202009-10-05 17:07:12 -0700343status_t GraphicBuffer::unlock()
344{
John Reck434bc982023-12-19 17:04:07 -0500345 return unlockAsync(nullptr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700346}
347
Valerie Haub94adfd2019-02-07 14:25:12 -0800348status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd,
349 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Francis Hart8f396012014-04-01 15:30:53 +0300350 const Rect lockBounds(width, height);
Valerie Haub94adfd2019-02-07 14:25:12 -0800351 status_t res =
352 lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Francis Hart8f396012014-04-01 15:30:53 +0300353 return res;
354}
355
Valerie Haub94adfd2019-02-07 14:25:12 -0800356status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd,
357 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
358 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Craig Donnere96a3252017-02-02 12:13:34 -0800359}
360
Valerie Haub94adfd2019-02-07 14:25:12 -0800361status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
362 const Rect& rect, void** vaddr, int fenceFd,
363 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800364 if (rect.left < 0 || rect.right > width ||
365 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300366 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
367 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800368 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300369 return BAD_VALUE;
370 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800371
John Reck434bc982023-12-19 17:04:07 -0500372 // Resolve the bpp & bps before doing a lock in case this fails we don't have to worry about
373 // doing an unlock
374 int32_t legacyBpp = -1, legacyBps = -1;
375 if (outBytesPerPixel || outBytesPerStride) {
376 const auto mapperVersion = getBufferMapperVersion();
377 // For gralloc2 we need to guess at the bpp & bps
378 // For gralloc3 the lock() call will return it
379 // For gralloc4 & later the PlaneLayout metadata query is vastly superior and we
380 // resolve bpp & bps just for compatibility
Valerie Haub94adfd2019-02-07 14:25:12 -0800381
John Reck434bc982023-12-19 17:04:07 -0500382 // TODO: See if we can't just remove gralloc2 support.
383 if (mapperVersion == GraphicBufferMapper::GRALLOC_2) {
384 legacyBpp = bytesPerPixel(format);
385 if (legacyBpp > 0) {
386 legacyBps = stride * legacyBpp;
387 } else {
388 legacyBpp = -1;
389 }
390 } else if (mapperVersion >= GraphicBufferMapper::GRALLOC_4) {
391 auto planeLayout = getBufferMapper().getPlaneLayouts(handle);
392 if (!planeLayout.has_value()) return planeLayout.asStatus();
393 resolveLegacyByteLayoutFromPlaneLayout(planeLayout.value(), &legacyBpp, &legacyBps);
394 }
395 }
396
Fang Huia3d73332024-07-22 22:11:40 +0800397 const uint64_t usage = static_cast<uint64_t>(ANDROID_NATIVE_UNSIGNED_CAST(
398 android_convertGralloc1To0Usage(inProducerUsage, inConsumerUsage)));
John Reck434bc982023-12-19 17:04:07 -0500399
400 auto result = getBufferMapper().lock(handle, usage, rect, base::unique_fd{fenceFd});
401
402 if (!result.has_value()) {
403 return result.error().asStatus();
404 }
405 auto value = result.value();
406 *vaddr = value.address;
407
408 if (outBytesPerPixel) {
409 *outBytesPerPixel = legacyBpp != -1 ? legacyBpp : value.bytesPerPixel;
410 }
411 if (outBytesPerStride) {
412 *outBytesPerStride = legacyBps != -1 ? legacyBps : value.bytesPerStride;
413 }
414 return OK;
Francis Hart8f396012014-04-01 15:30:53 +0300415}
416
Dan Stozad3182402014-11-17 12:03:59 -0800417status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
418 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300419{
420 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800421 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300422 return res;
423}
424
Dan Stozad3182402014-11-17 12:03:59 -0800425status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
426 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300427{
Dan Stozad3182402014-11-17 12:03:59 -0800428 if (rect.left < 0 || rect.right > width ||
429 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300430 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
431 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800432 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300433 return BAD_VALUE;
434 }
John Reck434bc982023-12-19 17:04:07 -0500435 auto result = getBufferMapper().lockYCbCr(handle, static_cast<int64_t>(inUsage), rect,
436 base::unique_fd{fenceFd});
437 if (!result.has_value()) {
438 return result.error().asStatus();
439 }
440 *ycbcr = result.value();
441 return OK;
Francis Hart8f396012014-04-01 15:30:53 +0300442}
443
444status_t GraphicBuffer::unlockAsync(int *fenceFd)
445{
John Reck434bc982023-12-19 17:04:07 -0500446 return getBufferMapper().unlockAsync(handle, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300447}
448
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800449status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
450 uint32_t inLayerCount, uint64_t inUsage,
451 bool* outSupported) const {
452 return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage,
453 outSupported);
454}
455
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800456size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700457 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800458}
Mathias Agopian3330b202009-10-05 17:07:12 -0700459
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800460size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700461 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800462}
463
Mathias Agopiane1424282013-07-29 21:24:40 -0700464status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800465 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
466 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700467
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800468 size_t fdCountNeeded = GraphicBuffer::getFdCount();
469 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700470
Dan Stozab1363d32014-03-28 15:10:52 -0700471 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700472 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800473 buf[1] = width;
474 buf[2] = height;
475 buf[3] = stride;
476 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700477 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700478 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700479 buf[7] = static_cast<int32_t>(mId >> 32);
480 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
481 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700482 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700483 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700484 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800485
486 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700487 buf[10] = int32_t(mTransportNumFds);
488 buf[11] = int32_t(mTransportNumInts);
489 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700490 memcpy(buf + 13, handle->data + handle->numFds,
Tianyu Jiang7791e642019-02-15 18:26:17 -0800491 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700492 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800493
Dan Stozaeea6d682015-04-20 12:07:13 -0700494 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700495 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700496 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700497 fds += mTransportNumFds;
498 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700499 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800500 return NO_ERROR;
501}
502
Tianyu Jiang7791e642019-02-15 18:26:17 -0800503status_t GraphicBuffer::unflatten(void const*& buffer, size_t& size, int const*& fds,
504 size_t& count) {
Tianyu Jiang69823cf2019-03-25 15:38:17 -0700505 // Check if size is not smaller than buf[0] is supposed to take.
506 if (size < sizeof(int)) {
507 return NO_MEMORY;
508 }
509
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800510 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700511
512 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
513 // see H2BGraphicBufferProducer.cpp
514 uint32_t flattenWordCount = 0;
515 if (buf[0] == 'GB01') {
516 // new version with 64-bits usage bits
517 flattenWordCount = 13;
518 } else if (buf[0] == 'GBFR') {
519 // old version, when usage bits were 32-bits
520 flattenWordCount = 12;
521 } else {
522 return BAD_TYPE;
523 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800524
Tianyu Jiang7791e642019-02-15 18:26:17 -0800525 if (size < 12 * sizeof(int)) {
526 android_errorWriteLog(0x534e4554, "114223584");
527 return NO_MEMORY;
528 }
529
Craig Donner6ebc46a2016-10-21 15:23:44 -0700530 const size_t numFds = static_cast<size_t>(buf[10]);
531 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800532
Michael Lentinec168b8a2015-02-18 10:14:18 -0800533 // Limit the maxNumber to be relatively small. The number of fds or ints
534 // should not come close to this number, and the number itself was simply
535 // chosen to be high enough to not cause issues and low enough to prevent
536 // overflow problems.
537 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700538 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
539 width = height = stride = format = usage_deprecated = 0;
540 layerCount = 0;
541 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700542 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700543 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700544 return BAD_VALUE;
545 }
546
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700547 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800548 if (size < sizeNeeded) return NO_MEMORY;
549
Michael Lentine38803262014-10-31 15:25:03 -0700550 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800551 if (count < fdCountNeeded) return NO_MEMORY;
552
553 if (handle) {
554 // free previous handle if any
555 free_handle();
556 }
557
558 if (numFds || numInts) {
559 width = buf[1];
560 height = buf[2];
561 stride = buf[3];
562 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700563 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700564 usage_deprecated = buf[6];
565 if (flattenWordCount == 13) {
566 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
567 } else {
Jessie Hao5480b212022-09-02 12:36:56 +0800568 usage = uint64_t(ANDROID_NATIVE_UNSIGNED_CAST(usage_deprecated));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700569 }
Tianyu Jiang7791e642019-02-15 18:26:17 -0800570 native_handle* h =
571 native_handle_create(static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700572 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700573 width = height = stride = format = usage_deprecated = 0;
574 layerCount = 0;
575 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700576 handle = nullptr;
Michael Lentine38803262014-10-31 15:25:03 -0700577 ALOGE("unflatten: native_handle_create failed");
578 return NO_MEMORY;
579 }
Dan Stozad3182402014-11-17 12:03:59 -0800580 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700581 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800582 handle = h;
583 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700584 width = height = stride = format = usage_deprecated = 0;
585 layerCount = 0;
586 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700587 handle = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800588 }
589
Craig Donner6ebc46a2016-10-21 15:23:44 -0700590 mId = static_cast<uint64_t>(buf[7]) << 32;
591 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700592
Craig Donner6ebc46a2016-10-21 15:23:44 -0700593 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700594
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800595 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700596
Yi Kong48d76082019-03-24 02:01:06 -0700597 if (handle != nullptr) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700598 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700599 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
600 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700601 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700602 width = height = stride = format = usage_deprecated = 0;
603 layerCount = 0;
604 usage = 0;
Xiao Zhang23349ea2024-11-29 09:08:16 +0000605 native_handle_close(handle);
606 native_handle_delete(const_cast<native_handle_t*>(handle));
Yi Kong48d76082019-03-24 02:01:06 -0700607 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700608 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700609 return err;
610 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700611
612 native_handle_close(handle);
613 native_handle_delete(const_cast<native_handle_t*>(handle));
614 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700615 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700616 }
617
Alec Mouridf868ba2025-02-25 16:08:19 -0800618 std::string name;
619 status_t err = mBufferMapper.getName(handle, &name);
620 if (err != NO_ERROR) {
621 name = "<Unknown>";
622 }
623
624 mDependencyMonitor.setToken(name.append(":").append(std::to_string(mId)));
625
Dan Stozaeea6d682015-04-20 12:07:13 -0700626 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700627 size -= sizeNeeded;
628 fds += numFds;
629 count -= numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800630 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700631}
632
Marissa Wall78b72202019-03-15 14:58:34 -0700633void GraphicBuffer::addDeathCallback(GraphicBufferDeathCallback deathCallback, void* context) {
634 mDeathCallbacks.emplace_back(deathCallback, context);
635}
636
Mathias Agopian3330b202009-10-05 17:07:12 -0700637// ---------------------------------------------------------------------------
638
639}; // namespace android