blob: c007fdb587b3cf79485525a69e9364677dc9057d [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>
25
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
Mathias Agopianf543e5a2017-04-03 17:16:41 -070043sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
44 return static_cast<GraphicBuffer *>(anwb);
45}
46
Pawin Vongmasae672cd02019-02-14 16:01:29 -080047GraphicBuffer* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer* buffer) {
48 return reinterpret_cast<GraphicBuffer*>(buffer);
49}
50
51GraphicBuffer const* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer const* buffer) {
52 return reinterpret_cast<GraphicBuffer const*>(buffer);
53}
54
55AHardwareBuffer* GraphicBuffer::toAHardwareBuffer() {
56 return reinterpret_cast<AHardwareBuffer*>(this);
57}
58
59AHardwareBuffer const* GraphicBuffer::toAHardwareBuffer() const {
60 return reinterpret_cast<AHardwareBuffer const*>(this);
61}
62
Mathias Agopian3330b202009-10-05 17:07:12 -070063GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070064 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070065 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070066{
Dan Stoza01049c82014-11-11 10:32:31 -080067 width =
68 height =
69 stride =
70 format =
Mathias Agopiancb496ac2017-05-22 14:21:00 -070071 usage_deprecated = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070072 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080073 layerCount = 0;
Yi Kong48d76082019-03-24 02:01:06 -070074 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -070075}
76
Chia-I Wub42f1712017-03-21 13:15:39 -070077// deprecated
Dan Stozad3182402014-11-17 12:03:59 -080078GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070079 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopiancb496ac2017-05-22 14:21:00 -070080 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -070081{
Mathias Agopian3330b202009-10-05 17:07:12 -070082}
83
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -080084GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
85 uint32_t inLayerCount, uint64_t inUsage, std::string requestorName)
86 : GraphicBuffer() {
87 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage,
88 std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070089}
90
Chia-I Wub42f1712017-03-21 13:15:39 -070091// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -070092GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
93 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
94 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -070095 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
Chris Forbes82c04982017-04-19 14:29:54 -070096 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
Chia-I Wub42f1712017-03-21 13:15:39 -070097 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070098{
Mathias Agopian54ba51d2009-10-26 20:12:37 -070099}
100
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800101GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method,
102 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
103 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride)
104 : GraphicBuffer() {
105 mInitCheck = initWithHandle(inHandle, method, inWidth, inHeight, inFormat, inLayerCount,
106 inUsage, inStride);
Chia-I Wub42f1712017-03-21 13:15:39 -0700107}
108
John Reckd727e9c2023-12-08 11:30:37 -0500109GraphicBuffer::GraphicBuffer(const GraphicBufferAllocator::AllocationRequest& request)
110 : GraphicBuffer() {
111 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
112 auto result = allocator.allocate(request);
113 mInitCheck = result.status;
114 if (result.status == NO_ERROR) {
115 handle = result.handle;
116 stride = result.stride;
117
118 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
119
120 width = static_cast<int>(request.width);
121 height = static_cast<int>(request.height);
122 format = request.format;
123 layerCount = request.layerCount;
124 usage = request.usage;
125 usage_deprecated = int(usage);
126 }
127}
128
Mathias Agopian3330b202009-10-05 17:07:12 -0700129GraphicBuffer::~GraphicBuffer()
130{
Alec Mouri6338c9d2019-02-07 16:57:51 -0800131 ATRACE_CALL();
Mathias Agopian3330b202009-10-05 17:07:12 -0700132 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800133 free_handle();
134 }
Marissa Wall78b72202019-03-15 14:58:34 -0700135 for (auto& [callback, context] : mDeathCallbacks) {
136 callback(context, mId);
137 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800138}
139
140void GraphicBuffer::free_handle()
141{
142 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700143 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800144 } else if (mOwner == ownData) {
145 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
146 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700147 }
Yi Kong48d76082019-03-24 02:01:06 -0700148 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700149}
150
151status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800152 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700153}
154
Mathias Agopian678bdd62010-12-03 17:33:09 -0800155void GraphicBuffer::dumpAllocationsToSystemLog()
156{
157 GraphicBufferAllocator::dumpToSystemLog();
158}
159
Iliyan Malchev697526b2011-05-01 11:33:26 -0700160ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700161{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700162 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700163 const_cast<GraphicBuffer*>(this));
164}
165
John Reckd727e9c2023-12-08 11:30:37 -0500166status_t GraphicBuffer::getDataspace(ui::Dataspace* outDataspace) const {
167 return mBufferMapper.getDataspace(handle, outDataspace);
168}
169
Dan Stozad3182402014-11-17 12:03:59 -0800170status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700171 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700172{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700173 if (mOwner != ownData)
174 return INVALID_OPERATION;
175
Dan Stozad3182402014-11-17 12:03:59 -0800176 if (handle &&
177 static_cast<int>(inWidth) == width &&
178 static_cast<int>(inHeight) == height &&
179 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700180 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700181 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700182 return NO_ERROR;
183
Mathias Agopian3330b202009-10-05 17:07:12 -0700184 if (handle) {
185 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
186 allocator.free(handle);
Yi Kong48d76082019-03-24 02:01:06 -0700187 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700188 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700189 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700190}
191
Dan Stoza9de72932015-04-16 17:28:43 -0700192bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700193 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700194{
195 if (static_cast<int>(inWidth) != width) return true;
196 if (static_cast<int>(inHeight) != height) return true;
197 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700198 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700199 if ((usage & inUsage) != inUsage) return true;
Peiyong Lin5d2018a2019-03-26 15:07:54 -0700200 if ((usage & USAGE_PROTECTED) != (inUsage & USAGE_PROTECTED)) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700201 return false;
202}
203
Chia-I Wub42f1712017-03-21 13:15:39 -0700204status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700205 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
206 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700207{
Mathias Agopian3330b202009-10-05 17:07:12 -0700208 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800209 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700210 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700211 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800212 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700213 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700214 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
215
Dan Stozad3182402014-11-17 12:03:59 -0800216 width = static_cast<int>(inWidth);
217 height = static_cast<int>(inHeight);
218 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700219 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700220 usage = inUsage;
221 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800222 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700223 }
224 return err;
225}
226
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800227status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
228 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
229 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) {
230 ANativeWindowBuffer::width = static_cast<int>(inWidth);
231 ANativeWindowBuffer::height = static_cast<int>(inHeight);
232 ANativeWindowBuffer::stride = static_cast<int>(inStride);
233 ANativeWindowBuffer::format = inFormat;
234 ANativeWindowBuffer::usage = inUsage;
235 ANativeWindowBuffer::usage_deprecated = int(inUsage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700236
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800237 ANativeWindowBuffer::layerCount = inLayerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700238
239 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
240
Chia-I Wucb8405e2017-04-17 15:20:19 -0700241 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
242 buffer_handle_t importedHandle;
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800243 status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount,
244 inFormat, inUsage, inStride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700245 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700246 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700247
248 return err;
249 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700250
251 if (method == TAKE_UNREGISTERED_HANDLE) {
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800252 native_handle_close(inHandle);
253 native_handle_delete(const_cast<native_handle_t*>(inHandle));
Chia-I Wucb8405e2017-04-17 15:20:19 -0700254 }
255
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800256 inHandle = importedHandle;
257 mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700258 }
259
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800260 ANativeWindowBuffer::handle = inHandle;
Chia-I Wucb8405e2017-04-17 15:20:19 -0700261
Chia-I Wub42f1712017-03-21 13:15:39 -0700262 return NO_ERROR;
263}
264
Valerie Hau250c6542019-01-31 14:23:43 -0800265status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel,
266 int32_t* outBytesPerStride) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700267 const Rect lockBounds(width, height);
Valerie Hau250c6542019-01-31 14:23:43 -0800268 status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700269 return res;
270}
271
Valerie Hau250c6542019-01-31 14:23:43 -0800272status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr,
273 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800274 if (rect.left < 0 || rect.right > width ||
275 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000276 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800277 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800278 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700279 return BAD_VALUE;
280 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800281
Valerie Hau250c6542019-01-31 14:23:43 -0800282 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr, outBytesPerPixel,
283 outBytesPerStride);
284
Mathias Agopian3330b202009-10-05 17:07:12 -0700285 return res;
286}
287
Dan Stozad3182402014-11-17 12:03:59 -0800288status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700289{
290 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800291 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700292 return res;
293}
294
Dan Stozad3182402014-11-17 12:03:59 -0800295status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
296 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700297{
Dan Stozad3182402014-11-17 12:03:59 -0800298 if (rect.left < 0 || rect.right > width ||
299 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700300 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
301 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800302 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700303 return BAD_VALUE;
304 }
Dan Stozad3182402014-11-17 12:03:59 -0800305 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700306 return res;
307}
308
Mathias Agopian3330b202009-10-05 17:07:12 -0700309status_t GraphicBuffer::unlock()
310{
311 status_t res = getBufferMapper().unlock(handle);
312 return res;
313}
314
Valerie Haub94adfd2019-02-07 14:25:12 -0800315status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd,
316 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Francis Hart8f396012014-04-01 15:30:53 +0300317 const Rect lockBounds(width, height);
Valerie Haub94adfd2019-02-07 14:25:12 -0800318 status_t res =
319 lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Francis Hart8f396012014-04-01 15:30:53 +0300320 return res;
321}
322
Valerie Haub94adfd2019-02-07 14:25:12 -0800323status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd,
324 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
325 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Craig Donnere96a3252017-02-02 12:13:34 -0800326}
327
Valerie Haub94adfd2019-02-07 14:25:12 -0800328status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
329 const Rect& rect, void** vaddr, int fenceFd,
330 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800331 if (rect.left < 0 || rect.right > width ||
332 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300333 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
334 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800335 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300336 return BAD_VALUE;
337 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800338
Valerie Hau0c9fc362019-01-22 09:17:19 -0800339 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect,
Valerie Haub94adfd2019-02-07 14:25:12 -0800340 vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
341
Francis Hart8f396012014-04-01 15:30:53 +0300342 return res;
343}
344
Dan Stozad3182402014-11-17 12:03:59 -0800345status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
346 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300347{
348 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800349 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300350 return res;
351}
352
Dan Stozad3182402014-11-17 12:03:59 -0800353status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
354 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300355{
Dan Stozad3182402014-11-17 12:03:59 -0800356 if (rect.left < 0 || rect.right > width ||
357 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300358 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
359 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800360 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300361 return BAD_VALUE;
362 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700363 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300364 return res;
365}
366
367status_t GraphicBuffer::unlockAsync(int *fenceFd)
368{
369 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
370 return res;
371}
372
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800373status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
374 uint32_t inLayerCount, uint64_t inUsage,
375 bool* outSupported) const {
376 return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage,
377 outSupported);
378}
379
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800380size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700381 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800382}
Mathias Agopian3330b202009-10-05 17:07:12 -0700383
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800384size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700385 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800386}
387
Mathias Agopiane1424282013-07-29 21:24:40 -0700388status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800389 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
390 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700391
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800392 size_t fdCountNeeded = GraphicBuffer::getFdCount();
393 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700394
Dan Stozab1363d32014-03-28 15:10:52 -0700395 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700396 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800397 buf[1] = width;
398 buf[2] = height;
399 buf[3] = stride;
400 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700401 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700402 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700403 buf[7] = static_cast<int32_t>(mId >> 32);
404 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
405 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700406 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700407 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700408 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800409
410 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700411 buf[10] = int32_t(mTransportNumFds);
412 buf[11] = int32_t(mTransportNumInts);
413 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700414 memcpy(buf + 13, handle->data + handle->numFds,
Tianyu Jiang7791e642019-02-15 18:26:17 -0800415 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700416 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800417
Dan Stozaeea6d682015-04-20 12:07:13 -0700418 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700419 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700420 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700421 fds += mTransportNumFds;
422 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700423 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800424 return NO_ERROR;
425}
426
Tianyu Jiang7791e642019-02-15 18:26:17 -0800427status_t GraphicBuffer::unflatten(void const*& buffer, size_t& size, int const*& fds,
428 size_t& count) {
Tianyu Jiang69823cf2019-03-25 15:38:17 -0700429 // Check if size is not smaller than buf[0] is supposed to take.
430 if (size < sizeof(int)) {
431 return NO_MEMORY;
432 }
433
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800434 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700435
436 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
437 // see H2BGraphicBufferProducer.cpp
438 uint32_t flattenWordCount = 0;
439 if (buf[0] == 'GB01') {
440 // new version with 64-bits usage bits
441 flattenWordCount = 13;
442 } else if (buf[0] == 'GBFR') {
443 // old version, when usage bits were 32-bits
444 flattenWordCount = 12;
445 } else {
446 return BAD_TYPE;
447 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800448
Tianyu Jiang7791e642019-02-15 18:26:17 -0800449 if (size < 12 * sizeof(int)) {
450 android_errorWriteLog(0x534e4554, "114223584");
451 return NO_MEMORY;
452 }
453
Craig Donner6ebc46a2016-10-21 15:23:44 -0700454 const size_t numFds = static_cast<size_t>(buf[10]);
455 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800456
Michael Lentinec168b8a2015-02-18 10:14:18 -0800457 // Limit the maxNumber to be relatively small. The number of fds or ints
458 // should not come close to this number, and the number itself was simply
459 // chosen to be high enough to not cause issues and low enough to prevent
460 // overflow problems.
461 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700462 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
463 width = height = stride = format = usage_deprecated = 0;
464 layerCount = 0;
465 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700466 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700467 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700468 return BAD_VALUE;
469 }
470
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700471 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800472 if (size < sizeNeeded) return NO_MEMORY;
473
Michael Lentine38803262014-10-31 15:25:03 -0700474 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800475 if (count < fdCountNeeded) return NO_MEMORY;
476
477 if (handle) {
478 // free previous handle if any
479 free_handle();
480 }
481
482 if (numFds || numInts) {
483 width = buf[1];
484 height = buf[2];
485 stride = buf[3];
486 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700487 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700488 usage_deprecated = buf[6];
489 if (flattenWordCount == 13) {
490 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
491 } else {
Jessie Hao5480b212022-09-02 12:36:56 +0800492 usage = uint64_t(ANDROID_NATIVE_UNSIGNED_CAST(usage_deprecated));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700493 }
Tianyu Jiang7791e642019-02-15 18:26:17 -0800494 native_handle* h =
495 native_handle_create(static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700496 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700497 width = height = stride = format = usage_deprecated = 0;
498 layerCount = 0;
499 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700500 handle = nullptr;
Michael Lentine38803262014-10-31 15:25:03 -0700501 ALOGE("unflatten: native_handle_create failed");
502 return NO_MEMORY;
503 }
Dan Stozad3182402014-11-17 12:03:59 -0800504 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700505 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800506 handle = h;
507 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700508 width = height = stride = format = usage_deprecated = 0;
509 layerCount = 0;
510 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700511 handle = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800512 }
513
Craig Donner6ebc46a2016-10-21 15:23:44 -0700514 mId = static_cast<uint64_t>(buf[7]) << 32;
515 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700516
Craig Donner6ebc46a2016-10-21 15:23:44 -0700517 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700518
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800519 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700520
Yi Kong48d76082019-03-24 02:01:06 -0700521 if (handle != nullptr) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700522 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700523 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
524 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700525 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700526 width = height = stride = format = usage_deprecated = 0;
527 layerCount = 0;
528 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700529 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700530 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700531 return err;
532 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700533
534 native_handle_close(handle);
535 native_handle_delete(const_cast<native_handle_t*>(handle));
536 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700537 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700538 }
539
Dan Stozaeea6d682015-04-20 12:07:13 -0700540 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700541 size -= sizeNeeded;
542 fds += numFds;
543 count -= numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800544 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700545}
546
Marissa Wall78b72202019-03-15 14:58:34 -0700547void GraphicBuffer::addDeathCallback(GraphicBufferDeathCallback deathCallback, void* context) {
548 mDeathCallbacks.emplace_back(deathCallback, context);
549}
550
Mathias Agopian3330b202009-10-05 17:07:12 -0700551// ---------------------------------------------------------------------------
552
553}; // namespace android