blob: b0c6e44b2b447f2db9d51b9244349ac94d6528bb [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
Mathias Agopian3330b202009-10-05 17:07:12 -070030namespace android {
31
32// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070033// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070034// ===========================================================================
35
Dan Stozab1363d32014-03-28 15:10:52 -070036static uint64_t getUniqueId() {
37 static volatile int32_t nextId = 0;
38 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
39 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
40 return id;
41}
42
John Reck434bc982023-12-19 17:04:07 -050043static void resolveLegacyByteLayoutFromPlaneLayout(const std::vector<ui::PlaneLayout>& planeLayouts,
44 int32_t* outBytesPerPixel,
45 int32_t* outBytesPerStride) {
46 if (planeLayouts.empty()) return;
47 if (outBytesPerPixel) {
48 int32_t bitsPerPixel = planeLayouts.front().sampleIncrementInBits;
49 for (const auto& planeLayout : planeLayouts) {
50 if (bitsPerPixel != planeLayout.sampleIncrementInBits) {
51 bitsPerPixel = -1;
52 }
53 }
54 if (bitsPerPixel >= 0 && bitsPerPixel % 8 == 0) {
55 *outBytesPerPixel = bitsPerPixel / 8;
56 } else {
57 *outBytesPerPixel = -1;
58 }
59 }
60 if (outBytesPerStride) {
61 int32_t bytesPerStride = planeLayouts.front().strideInBytes;
62 for (const auto& planeLayout : planeLayouts) {
63 if (bytesPerStride != planeLayout.strideInBytes) {
64 bytesPerStride = -1;
65 }
66 }
67 if (bytesPerStride >= 0) {
68 *outBytesPerStride = bytesPerStride;
69 } else {
70 *outBytesPerStride = -1;
71 }
72 }
73}
74
Mathias Agopianf543e5a2017-04-03 17:16:41 -070075sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
76 return static_cast<GraphicBuffer *>(anwb);
77}
78
Pawin Vongmasae672cd02019-02-14 16:01:29 -080079GraphicBuffer* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer* buffer) {
80 return reinterpret_cast<GraphicBuffer*>(buffer);
81}
82
83GraphicBuffer const* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer const* buffer) {
84 return reinterpret_cast<GraphicBuffer const*>(buffer);
85}
86
87AHardwareBuffer* GraphicBuffer::toAHardwareBuffer() {
88 return reinterpret_cast<AHardwareBuffer*>(this);
89}
90
91AHardwareBuffer const* GraphicBuffer::toAHardwareBuffer() const {
92 return reinterpret_cast<AHardwareBuffer const*>(this);
93}
94
Mathias Agopian3330b202009-10-05 17:07:12 -070095GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070096 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070097 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070098{
Dan Stoza01049c82014-11-11 10:32:31 -080099 width =
100 height =
101 stride =
102 format =
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700103 usage_deprecated = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700104 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -0800105 layerCount = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700106 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700107}
108
Chia-I Wub42f1712017-03-21 13:15:39 -0700109// deprecated
Dan Stozad3182402014-11-17 12:03:59 -0800110GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -0700111 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700112 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700113{
Mathias Agopian3330b202009-10-05 17:07:12 -0700114}
115
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800116GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
117 uint32_t inLayerCount, uint64_t inUsage, std::string requestorName)
118 : GraphicBuffer() {
119 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage,
120 std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700121}
122
Chia-I Wub42f1712017-03-21 13:15:39 -0700123// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -0700124GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
125 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
126 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -0700127 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
Chris Forbes82c04982017-04-19 14:29:54 -0700128 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
Chia-I Wub42f1712017-03-21 13:15:39 -0700129 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700130{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700131}
132
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800133GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method,
134 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
135 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride)
136 : GraphicBuffer() {
137 mInitCheck = initWithHandle(inHandle, method, inWidth, inHeight, inFormat, inLayerCount,
138 inUsage, inStride);
Chia-I Wub42f1712017-03-21 13:15:39 -0700139}
140
John Reckd727e9c2023-12-08 11:30:37 -0500141GraphicBuffer::GraphicBuffer(const GraphicBufferAllocator::AllocationRequest& request)
142 : GraphicBuffer() {
143 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
144 auto result = allocator.allocate(request);
145 mInitCheck = result.status;
146 if (result.status == NO_ERROR) {
147 handle = result.handle;
148 stride = result.stride;
149
150 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
151
152 width = static_cast<int>(request.width);
153 height = static_cast<int>(request.height);
154 format = request.format;
155 layerCount = request.layerCount;
156 usage = request.usage;
157 usage_deprecated = int(usage);
158 }
159}
160
Mathias Agopian3330b202009-10-05 17:07:12 -0700161GraphicBuffer::~GraphicBuffer()
162{
Alec Mouri6338c9d2019-02-07 16:57:51 -0800163 ATRACE_CALL();
Mathias Agopian3330b202009-10-05 17:07:12 -0700164 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800165 free_handle();
166 }
Marissa Wall78b72202019-03-15 14:58:34 -0700167 for (auto& [callback, context] : mDeathCallbacks) {
168 callback(context, mId);
169 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800170}
171
172void GraphicBuffer::free_handle()
173{
174 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700175 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800176 } else if (mOwner == ownData) {
177 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
178 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700179 }
Yi Kong48d76082019-03-24 02:01:06 -0700180 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700181}
182
183status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800184 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700185}
186
Mathias Agopian678bdd62010-12-03 17:33:09 -0800187void GraphicBuffer::dumpAllocationsToSystemLog()
188{
189 GraphicBufferAllocator::dumpToSystemLog();
190}
191
Iliyan Malchev697526b2011-05-01 11:33:26 -0700192ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700193{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700194 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700195 const_cast<GraphicBuffer*>(this));
196}
197
John Reckd727e9c2023-12-08 11:30:37 -0500198status_t GraphicBuffer::getDataspace(ui::Dataspace* outDataspace) const {
199 return mBufferMapper.getDataspace(handle, outDataspace);
200}
201
Dan Stozad3182402014-11-17 12:03:59 -0800202status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700203 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700204{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700205 if (mOwner != ownData)
206 return INVALID_OPERATION;
207
Dan Stozad3182402014-11-17 12:03:59 -0800208 if (handle &&
209 static_cast<int>(inWidth) == width &&
210 static_cast<int>(inHeight) == height &&
211 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700212 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700213 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700214 return NO_ERROR;
215
Mathias Agopian3330b202009-10-05 17:07:12 -0700216 if (handle) {
217 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
218 allocator.free(handle);
Yi Kong48d76082019-03-24 02:01:06 -0700219 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700220 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700221 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700222}
223
Dan Stoza9de72932015-04-16 17:28:43 -0700224bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700225 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700226{
227 if (static_cast<int>(inWidth) != width) return true;
228 if (static_cast<int>(inHeight) != height) return true;
229 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700230 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700231 if ((usage & inUsage) != inUsage) return true;
Peiyong Lin5d2018a2019-03-26 15:07:54 -0700232 if ((usage & USAGE_PROTECTED) != (inUsage & USAGE_PROTECTED)) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700233 return false;
234}
235
Chia-I Wub42f1712017-03-21 13:15:39 -0700236status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700237 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
238 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700239{
Mathias Agopian3330b202009-10-05 17:07:12 -0700240 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800241 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700242 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700243 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800244 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700245 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700246 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
247
Dan Stozad3182402014-11-17 12:03:59 -0800248 width = static_cast<int>(inWidth);
249 height = static_cast<int>(inHeight);
250 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700251 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700252 usage = inUsage;
253 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800254 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700255 }
256 return err;
257}
258
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800259status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
260 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
261 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) {
262 ANativeWindowBuffer::width = static_cast<int>(inWidth);
263 ANativeWindowBuffer::height = static_cast<int>(inHeight);
264 ANativeWindowBuffer::stride = static_cast<int>(inStride);
265 ANativeWindowBuffer::format = inFormat;
266 ANativeWindowBuffer::usage = inUsage;
267 ANativeWindowBuffer::usage_deprecated = int(inUsage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700268
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800269 ANativeWindowBuffer::layerCount = inLayerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700270
271 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
272
Chia-I Wucb8405e2017-04-17 15:20:19 -0700273 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
274 buffer_handle_t importedHandle;
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800275 status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount,
276 inFormat, inUsage, inStride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700277 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700278 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700279
280 return err;
281 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700282
283 if (method == TAKE_UNREGISTERED_HANDLE) {
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800284 native_handle_close(inHandle);
285 native_handle_delete(const_cast<native_handle_t*>(inHandle));
Chia-I Wucb8405e2017-04-17 15:20:19 -0700286 }
287
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800288 inHandle = importedHandle;
289 mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700290 }
291
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800292 ANativeWindowBuffer::handle = inHandle;
Chia-I Wucb8405e2017-04-17 15:20:19 -0700293
Chia-I Wub42f1712017-03-21 13:15:39 -0700294 return NO_ERROR;
295}
296
Valerie Hau250c6542019-01-31 14:23:43 -0800297status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel,
298 int32_t* outBytesPerStride) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700299 const Rect lockBounds(width, height);
Valerie Hau250c6542019-01-31 14:23:43 -0800300 status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700301 return res;
302}
303
Valerie Hau250c6542019-01-31 14:23:43 -0800304status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr,
305 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800306 if (rect.left < 0 || rect.right > width ||
307 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000308 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800309 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800310 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700311 return BAD_VALUE;
312 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800313
John Reck434bc982023-12-19 17:04:07 -0500314 return lockAsync(inUsage, rect, vaddr, -1, outBytesPerPixel, outBytesPerStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700315}
316
Dan Stozad3182402014-11-17 12:03:59 -0800317status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700318{
319 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800320 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700321 return res;
322}
323
Dan Stozad3182402014-11-17 12:03:59 -0800324status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
325 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700326{
Dan Stozad3182402014-11-17 12:03:59 -0800327 if (rect.left < 0 || rect.right > width ||
328 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700329 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
330 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800331 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700332 return BAD_VALUE;
333 }
John Reck434bc982023-12-19 17:04:07 -0500334 return lockAsyncYCbCr(inUsage, rect, ycbcr, -1);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700335}
336
Mathias Agopian3330b202009-10-05 17:07:12 -0700337status_t GraphicBuffer::unlock()
338{
John Reck434bc982023-12-19 17:04:07 -0500339 return unlockAsync(nullptr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700340}
341
Valerie Haub94adfd2019-02-07 14:25:12 -0800342status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd,
343 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Francis Hart8f396012014-04-01 15:30:53 +0300344 const Rect lockBounds(width, height);
Valerie Haub94adfd2019-02-07 14:25:12 -0800345 status_t res =
346 lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Francis Hart8f396012014-04-01 15:30:53 +0300347 return res;
348}
349
Valerie Haub94adfd2019-02-07 14:25:12 -0800350status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd,
351 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
352 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Craig Donnere96a3252017-02-02 12:13:34 -0800353}
354
Valerie Haub94adfd2019-02-07 14:25:12 -0800355status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
356 const Rect& rect, void** vaddr, int fenceFd,
357 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800358 if (rect.left < 0 || rect.right > width ||
359 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300360 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
361 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800362 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300363 return BAD_VALUE;
364 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800365
John Reck434bc982023-12-19 17:04:07 -0500366 // Resolve the bpp & bps before doing a lock in case this fails we don't have to worry about
367 // doing an unlock
368 int32_t legacyBpp = -1, legacyBps = -1;
369 if (outBytesPerPixel || outBytesPerStride) {
370 const auto mapperVersion = getBufferMapperVersion();
371 // For gralloc2 we need to guess at the bpp & bps
372 // For gralloc3 the lock() call will return it
373 // For gralloc4 & later the PlaneLayout metadata query is vastly superior and we
374 // resolve bpp & bps just for compatibility
Valerie Haub94adfd2019-02-07 14:25:12 -0800375
John Reck434bc982023-12-19 17:04:07 -0500376 // TODO: See if we can't just remove gralloc2 support.
377 if (mapperVersion == GraphicBufferMapper::GRALLOC_2) {
378 legacyBpp = bytesPerPixel(format);
379 if (legacyBpp > 0) {
380 legacyBps = stride * legacyBpp;
381 } else {
382 legacyBpp = -1;
383 }
384 } else if (mapperVersion >= GraphicBufferMapper::GRALLOC_4) {
385 auto planeLayout = getBufferMapper().getPlaneLayouts(handle);
386 if (!planeLayout.has_value()) return planeLayout.asStatus();
387 resolveLegacyByteLayoutFromPlaneLayout(planeLayout.value(), &legacyBpp, &legacyBps);
388 }
389 }
390
Fang Huia3d73332024-07-22 22:11:40 +0800391 const uint64_t usage = static_cast<uint64_t>(ANDROID_NATIVE_UNSIGNED_CAST(
392 android_convertGralloc1To0Usage(inProducerUsage, inConsumerUsage)));
John Reck434bc982023-12-19 17:04:07 -0500393
394 auto result = getBufferMapper().lock(handle, usage, rect, base::unique_fd{fenceFd});
395
396 if (!result.has_value()) {
397 return result.error().asStatus();
398 }
399 auto value = result.value();
400 *vaddr = value.address;
401
402 if (outBytesPerPixel) {
403 *outBytesPerPixel = legacyBpp != -1 ? legacyBpp : value.bytesPerPixel;
404 }
405 if (outBytesPerStride) {
406 *outBytesPerStride = legacyBps != -1 ? legacyBps : value.bytesPerStride;
407 }
408 return OK;
Francis Hart8f396012014-04-01 15:30:53 +0300409}
410
Dan Stozad3182402014-11-17 12:03:59 -0800411status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
412 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300413{
414 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800415 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300416 return res;
417}
418
Dan Stozad3182402014-11-17 12:03:59 -0800419status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
420 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300421{
Dan Stozad3182402014-11-17 12:03:59 -0800422 if (rect.left < 0 || rect.right > width ||
423 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300424 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
425 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800426 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300427 return BAD_VALUE;
428 }
John Reck434bc982023-12-19 17:04:07 -0500429 auto result = getBufferMapper().lockYCbCr(handle, static_cast<int64_t>(inUsage), rect,
430 base::unique_fd{fenceFd});
431 if (!result.has_value()) {
432 return result.error().asStatus();
433 }
434 *ycbcr = result.value();
435 return OK;
Francis Hart8f396012014-04-01 15:30:53 +0300436}
437
438status_t GraphicBuffer::unlockAsync(int *fenceFd)
439{
John Reck434bc982023-12-19 17:04:07 -0500440 return getBufferMapper().unlockAsync(handle, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300441}
442
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800443status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
444 uint32_t inLayerCount, uint64_t inUsage,
445 bool* outSupported) const {
446 return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage,
447 outSupported);
448}
449
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800450size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700451 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800452}
Mathias Agopian3330b202009-10-05 17:07:12 -0700453
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800454size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700455 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800456}
457
Mathias Agopiane1424282013-07-29 21:24:40 -0700458status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800459 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
460 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700461
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800462 size_t fdCountNeeded = GraphicBuffer::getFdCount();
463 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700464
Dan Stozab1363d32014-03-28 15:10:52 -0700465 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700466 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800467 buf[1] = width;
468 buf[2] = height;
469 buf[3] = stride;
470 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700471 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700472 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700473 buf[7] = static_cast<int32_t>(mId >> 32);
474 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
475 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700476 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700477 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700478 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800479
480 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700481 buf[10] = int32_t(mTransportNumFds);
482 buf[11] = int32_t(mTransportNumInts);
483 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700484 memcpy(buf + 13, handle->data + handle->numFds,
Tianyu Jiang7791e642019-02-15 18:26:17 -0800485 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700486 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800487
Dan Stozaeea6d682015-04-20 12:07:13 -0700488 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700489 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700490 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700491 fds += mTransportNumFds;
492 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700493 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800494 return NO_ERROR;
495}
496
Tianyu Jiang7791e642019-02-15 18:26:17 -0800497status_t GraphicBuffer::unflatten(void const*& buffer, size_t& size, int const*& fds,
498 size_t& count) {
Tianyu Jiang69823cf2019-03-25 15:38:17 -0700499 // Check if size is not smaller than buf[0] is supposed to take.
500 if (size < sizeof(int)) {
501 return NO_MEMORY;
502 }
503
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800504 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700505
506 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
507 // see H2BGraphicBufferProducer.cpp
508 uint32_t flattenWordCount = 0;
509 if (buf[0] == 'GB01') {
510 // new version with 64-bits usage bits
511 flattenWordCount = 13;
512 } else if (buf[0] == 'GBFR') {
513 // old version, when usage bits were 32-bits
514 flattenWordCount = 12;
515 } else {
516 return BAD_TYPE;
517 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800518
Tianyu Jiang7791e642019-02-15 18:26:17 -0800519 if (size < 12 * sizeof(int)) {
520 android_errorWriteLog(0x534e4554, "114223584");
521 return NO_MEMORY;
522 }
523
Craig Donner6ebc46a2016-10-21 15:23:44 -0700524 const size_t numFds = static_cast<size_t>(buf[10]);
525 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800526
Michael Lentinec168b8a2015-02-18 10:14:18 -0800527 // Limit the maxNumber to be relatively small. The number of fds or ints
528 // should not come close to this number, and the number itself was simply
529 // chosen to be high enough to not cause issues and low enough to prevent
530 // overflow problems.
531 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700532 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
533 width = height = stride = format = usage_deprecated = 0;
534 layerCount = 0;
535 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700536 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700537 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700538 return BAD_VALUE;
539 }
540
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700541 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800542 if (size < sizeNeeded) return NO_MEMORY;
543
Michael Lentine38803262014-10-31 15:25:03 -0700544 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800545 if (count < fdCountNeeded) return NO_MEMORY;
546
547 if (handle) {
548 // free previous handle if any
549 free_handle();
550 }
551
552 if (numFds || numInts) {
553 width = buf[1];
554 height = buf[2];
555 stride = buf[3];
556 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700557 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700558 usage_deprecated = buf[6];
559 if (flattenWordCount == 13) {
560 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
561 } else {
Jessie Hao5480b212022-09-02 12:36:56 +0800562 usage = uint64_t(ANDROID_NATIVE_UNSIGNED_CAST(usage_deprecated));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700563 }
Tianyu Jiang7791e642019-02-15 18:26:17 -0800564 native_handle* h =
565 native_handle_create(static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700566 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700567 width = height = stride = format = usage_deprecated = 0;
568 layerCount = 0;
569 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700570 handle = nullptr;
Michael Lentine38803262014-10-31 15:25:03 -0700571 ALOGE("unflatten: native_handle_create failed");
572 return NO_MEMORY;
573 }
Dan Stozad3182402014-11-17 12:03:59 -0800574 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700575 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800576 handle = h;
577 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700578 width = height = stride = format = usage_deprecated = 0;
579 layerCount = 0;
580 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700581 handle = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800582 }
583
Craig Donner6ebc46a2016-10-21 15:23:44 -0700584 mId = static_cast<uint64_t>(buf[7]) << 32;
585 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700586
Craig Donner6ebc46a2016-10-21 15:23:44 -0700587 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700588
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800589 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700590
Yi Kong48d76082019-03-24 02:01:06 -0700591 if (handle != nullptr) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700592 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700593 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
594 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700595 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700596 width = height = stride = format = usage_deprecated = 0;
597 layerCount = 0;
598 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700599 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700600 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700601 return err;
602 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700603
604 native_handle_close(handle);
605 native_handle_delete(const_cast<native_handle_t*>(handle));
606 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700607 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700608 }
609
Dan Stozaeea6d682015-04-20 12:07:13 -0700610 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700611 size -= sizeNeeded;
612 fds += numFds;
613 count -= numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800614 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700615}
616
Marissa Wall78b72202019-03-15 14:58:34 -0700617void GraphicBuffer::addDeathCallback(GraphicBufferDeathCallback deathCallback, void* context) {
618 mDeathCallbacks.emplace_back(deathCallback, context);
619}
620
Mathias Agopian3330b202009-10-05 17:07:12 -0700621// ---------------------------------------------------------------------------
622
623}; // namespace android