blob: 429760ffe0eb30c0a46997edb44c6c3d8e14511a [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
Mathias Agopian3330b202009-10-05 17:07:12 -0700109GraphicBuffer::~GraphicBuffer()
110{
Alec Mouri6338c9d2019-02-07 16:57:51 -0800111 ATRACE_CALL();
Mathias Agopian3330b202009-10-05 17:07:12 -0700112 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800113 free_handle();
114 }
Marissa Wall78b72202019-03-15 14:58:34 -0700115 for (auto& [callback, context] : mDeathCallbacks) {
116 callback(context, mId);
117 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800118}
119
120void GraphicBuffer::free_handle()
121{
122 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700123 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800124 } else if (mOwner == ownData) {
125 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
126 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700127 }
Yi Kong48d76082019-03-24 02:01:06 -0700128 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700129}
130
131status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800132 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700133}
134
Mathias Agopian678bdd62010-12-03 17:33:09 -0800135void GraphicBuffer::dumpAllocationsToSystemLog()
136{
137 GraphicBufferAllocator::dumpToSystemLog();
138}
139
Iliyan Malchev697526b2011-05-01 11:33:26 -0700140ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700141{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700142 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700143 const_cast<GraphicBuffer*>(this));
144}
145
Dan Stozad3182402014-11-17 12:03:59 -0800146status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700147 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700148{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700149 if (mOwner != ownData)
150 return INVALID_OPERATION;
151
Dan Stozad3182402014-11-17 12:03:59 -0800152 if (handle &&
153 static_cast<int>(inWidth) == width &&
154 static_cast<int>(inHeight) == height &&
155 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700156 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700157 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700158 return NO_ERROR;
159
Mathias Agopian3330b202009-10-05 17:07:12 -0700160 if (handle) {
161 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
162 allocator.free(handle);
Yi Kong48d76082019-03-24 02:01:06 -0700163 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700164 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700165 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700166}
167
Dan Stoza9de72932015-04-16 17:28:43 -0700168bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700169 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700170{
171 if (static_cast<int>(inWidth) != width) return true;
172 if (static_cast<int>(inHeight) != height) return true;
173 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700174 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700175 if ((usage & inUsage) != inUsage) return true;
Peiyong Lin5d2018a2019-03-26 15:07:54 -0700176 if ((usage & USAGE_PROTECTED) != (inUsage & USAGE_PROTECTED)) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700177 return false;
178}
179
Chia-I Wub42f1712017-03-21 13:15:39 -0700180status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700181 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
182 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700183{
Mathias Agopian3330b202009-10-05 17:07:12 -0700184 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800185 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700186 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700187 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800188 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700189 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700190 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
191
Dan Stozad3182402014-11-17 12:03:59 -0800192 width = static_cast<int>(inWidth);
193 height = static_cast<int>(inHeight);
194 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700195 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700196 usage = inUsage;
197 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800198 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700199 }
200 return err;
201}
202
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800203status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
204 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
205 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) {
206 ANativeWindowBuffer::width = static_cast<int>(inWidth);
207 ANativeWindowBuffer::height = static_cast<int>(inHeight);
208 ANativeWindowBuffer::stride = static_cast<int>(inStride);
209 ANativeWindowBuffer::format = inFormat;
210 ANativeWindowBuffer::usage = inUsage;
211 ANativeWindowBuffer::usage_deprecated = int(inUsage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700212
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800213 ANativeWindowBuffer::layerCount = inLayerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700214
215 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
216
Chia-I Wucb8405e2017-04-17 15:20:19 -0700217 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
218 buffer_handle_t importedHandle;
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800219 status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount,
220 inFormat, inUsage, inStride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700221 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700222 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700223
224 return err;
225 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700226
227 if (method == TAKE_UNREGISTERED_HANDLE) {
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800228 native_handle_close(inHandle);
229 native_handle_delete(const_cast<native_handle_t*>(inHandle));
Chia-I Wucb8405e2017-04-17 15:20:19 -0700230 }
231
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800232 inHandle = importedHandle;
233 mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700234 }
235
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800236 ANativeWindowBuffer::handle = inHandle;
Chia-I Wucb8405e2017-04-17 15:20:19 -0700237
Chia-I Wub42f1712017-03-21 13:15:39 -0700238 return NO_ERROR;
239}
240
Valerie Hau250c6542019-01-31 14:23:43 -0800241status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel,
242 int32_t* outBytesPerStride) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700243 const Rect lockBounds(width, height);
Valerie Hau250c6542019-01-31 14:23:43 -0800244 status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700245 return res;
246}
247
Valerie Hau250c6542019-01-31 14:23:43 -0800248status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr,
249 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800250 if (rect.left < 0 || rect.right > width ||
251 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000252 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800253 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800254 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700255 return BAD_VALUE;
256 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800257
Valerie Hau250c6542019-01-31 14:23:43 -0800258 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr, outBytesPerPixel,
259 outBytesPerStride);
260
Mathias Agopian3330b202009-10-05 17:07:12 -0700261 return res;
262}
263
Dan Stozad3182402014-11-17 12:03:59 -0800264status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700265{
266 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800267 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700268 return res;
269}
270
Dan Stozad3182402014-11-17 12:03:59 -0800271status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
272 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700273{
Dan Stozad3182402014-11-17 12:03:59 -0800274 if (rect.left < 0 || rect.right > width ||
275 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700276 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
277 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800278 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700279 return BAD_VALUE;
280 }
Dan Stozad3182402014-11-17 12:03:59 -0800281 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700282 return res;
283}
284
Mathias Agopian3330b202009-10-05 17:07:12 -0700285status_t GraphicBuffer::unlock()
286{
287 status_t res = getBufferMapper().unlock(handle);
288 return res;
289}
290
Valerie Haub94adfd2019-02-07 14:25:12 -0800291status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd,
292 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Francis Hart8f396012014-04-01 15:30:53 +0300293 const Rect lockBounds(width, height);
Valerie Haub94adfd2019-02-07 14:25:12 -0800294 status_t res =
295 lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Francis Hart8f396012014-04-01 15:30:53 +0300296 return res;
297}
298
Valerie Haub94adfd2019-02-07 14:25:12 -0800299status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd,
300 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
301 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Craig Donnere96a3252017-02-02 12:13:34 -0800302}
303
Valerie Haub94adfd2019-02-07 14:25:12 -0800304status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
305 const Rect& rect, void** vaddr, int fenceFd,
306 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800307 if (rect.left < 0 || rect.right > width ||
308 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300309 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
310 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800311 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300312 return BAD_VALUE;
313 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800314
Valerie Hau0c9fc362019-01-22 09:17:19 -0800315 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect,
Valerie Haub94adfd2019-02-07 14:25:12 -0800316 vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
317
Francis Hart8f396012014-04-01 15:30:53 +0300318 return res;
319}
320
Dan Stozad3182402014-11-17 12:03:59 -0800321status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
322 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300323{
324 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800325 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300326 return res;
327}
328
Dan Stozad3182402014-11-17 12:03:59 -0800329status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
330 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300331{
Dan Stozad3182402014-11-17 12:03:59 -0800332 if (rect.left < 0 || rect.right > width ||
333 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300334 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
335 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800336 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300337 return BAD_VALUE;
338 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700339 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300340 return res;
341}
342
343status_t GraphicBuffer::unlockAsync(int *fenceFd)
344{
345 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
346 return res;
347}
348
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800349status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
350 uint32_t inLayerCount, uint64_t inUsage,
351 bool* outSupported) const {
352 return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage,
353 outSupported);
354}
355
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800356size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700357 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800358}
Mathias Agopian3330b202009-10-05 17:07:12 -0700359
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800360size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700361 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800362}
363
Mathias Agopiane1424282013-07-29 21:24:40 -0700364status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800365 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
366 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700367
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800368 size_t fdCountNeeded = GraphicBuffer::getFdCount();
369 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700370
Dan Stozab1363d32014-03-28 15:10:52 -0700371 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700372 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800373 buf[1] = width;
374 buf[2] = height;
375 buf[3] = stride;
376 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700377 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700378 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700379 buf[7] = static_cast<int32_t>(mId >> 32);
380 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
381 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700382 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700383 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700384 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800385
386 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700387 buf[10] = int32_t(mTransportNumFds);
388 buf[11] = int32_t(mTransportNumInts);
389 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700390 memcpy(buf + 13, handle->data + handle->numFds,
Tianyu Jiang7791e642019-02-15 18:26:17 -0800391 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700392 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800393
Dan Stozaeea6d682015-04-20 12:07:13 -0700394 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700395 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700396 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700397 fds += mTransportNumFds;
398 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700399 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800400 return NO_ERROR;
401}
402
Tianyu Jiang7791e642019-02-15 18:26:17 -0800403status_t GraphicBuffer::unflatten(void const*& buffer, size_t& size, int const*& fds,
404 size_t& count) {
Tianyu Jiang69823cf2019-03-25 15:38:17 -0700405 // Check if size is not smaller than buf[0] is supposed to take.
406 if (size < sizeof(int)) {
407 return NO_MEMORY;
408 }
409
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800410 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700411
412 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
413 // see H2BGraphicBufferProducer.cpp
414 uint32_t flattenWordCount = 0;
415 if (buf[0] == 'GB01') {
416 // new version with 64-bits usage bits
417 flattenWordCount = 13;
418 } else if (buf[0] == 'GBFR') {
419 // old version, when usage bits were 32-bits
420 flattenWordCount = 12;
421 } else {
422 return BAD_TYPE;
423 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800424
Tianyu Jiang7791e642019-02-15 18:26:17 -0800425 if (size < 12 * sizeof(int)) {
426 android_errorWriteLog(0x534e4554, "114223584");
427 return NO_MEMORY;
428 }
429
Craig Donner6ebc46a2016-10-21 15:23:44 -0700430 const size_t numFds = static_cast<size_t>(buf[10]);
431 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800432
Michael Lentinec168b8a2015-02-18 10:14:18 -0800433 // Limit the maxNumber to be relatively small. The number of fds or ints
434 // should not come close to this number, and the number itself was simply
435 // chosen to be high enough to not cause issues and low enough to prevent
436 // overflow problems.
437 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700438 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
439 width = height = stride = format = usage_deprecated = 0;
440 layerCount = 0;
441 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700442 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700443 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700444 return BAD_VALUE;
445 }
446
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700447 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800448 if (size < sizeNeeded) return NO_MEMORY;
449
Michael Lentine38803262014-10-31 15:25:03 -0700450 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800451 if (count < fdCountNeeded) return NO_MEMORY;
452
453 if (handle) {
454 // free previous handle if any
455 free_handle();
456 }
457
458 if (numFds || numInts) {
459 width = buf[1];
460 height = buf[2];
461 stride = buf[3];
462 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700463 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700464 usage_deprecated = buf[6];
465 if (flattenWordCount == 13) {
466 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
467 } else {
Jessie Hao5480b212022-09-02 12:36:56 +0800468 usage = uint64_t(ANDROID_NATIVE_UNSIGNED_CAST(usage_deprecated));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700469 }
Tianyu Jiang7791e642019-02-15 18:26:17 -0800470 native_handle* h =
471 native_handle_create(static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700472 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700473 width = height = stride = format = usage_deprecated = 0;
474 layerCount = 0;
475 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700476 handle = nullptr;
Michael Lentine38803262014-10-31 15:25:03 -0700477 ALOGE("unflatten: native_handle_create failed");
478 return NO_MEMORY;
479 }
Dan Stozad3182402014-11-17 12:03:59 -0800480 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700481 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800482 handle = h;
483 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700484 width = height = stride = format = usage_deprecated = 0;
485 layerCount = 0;
486 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700487 handle = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800488 }
489
Craig Donner6ebc46a2016-10-21 15:23:44 -0700490 mId = static_cast<uint64_t>(buf[7]) << 32;
491 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700492
Craig Donner6ebc46a2016-10-21 15:23:44 -0700493 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700494
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800495 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700496
Yi Kong48d76082019-03-24 02:01:06 -0700497 if (handle != nullptr) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700498 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700499 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
500 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700501 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700502 width = height = stride = format = usage_deprecated = 0;
503 layerCount = 0;
504 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700505 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700506 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700507 return err;
508 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700509
510 native_handle_close(handle);
511 native_handle_delete(const_cast<native_handle_t*>(handle));
512 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700513 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700514 }
515
Dan Stozaeea6d682015-04-20 12:07:13 -0700516 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700517 size -= sizeNeeded;
518 fds += numFds;
519 count -= numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800520 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700521}
522
Marissa Wall78b72202019-03-15 14:58:34 -0700523void GraphicBuffer::addDeathCallback(GraphicBufferDeathCallback deathCallback, void* context) {
524 mDeathCallbacks.emplace_back(deathCallback, context);
525}
526
Mathias Agopian3330b202009-10-05 17:07:12 -0700527// ---------------------------------------------------------------------------
528
529}; // namespace android