blob: 45340a1fe9f995fd51fa62cf69966129e03b4a2d [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
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -070026#ifndef LIBUI_IN_VNDK
27#include <ui/BufferHubBuffer.h>
28#endif // LIBUI_IN_VNDK
29
Chia-I Wu5bac7f32017-04-06 12:34:32 -070030#include <ui/Gralloc2.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070031#include <ui/GraphicBufferAllocator.h>
32#include <ui/GraphicBufferMapper.h>
Alec Mouri6338c9d2019-02-07 16:57:51 -080033#include <utils/Trace.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070034
Mathias Agopian3330b202009-10-05 17:07:12 -070035namespace android {
36
37// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070038// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070039// ===========================================================================
40
Dan Stozab1363d32014-03-28 15:10:52 -070041static uint64_t getUniqueId() {
42 static volatile int32_t nextId = 0;
43 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
44 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
45 return id;
46}
47
Mathias Agopianf543e5a2017-04-03 17:16:41 -070048sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
49 return static_cast<GraphicBuffer *>(anwb);
50}
51
Pawin Vongmasae672cd02019-02-14 16:01:29 -080052GraphicBuffer* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer* buffer) {
53 return reinterpret_cast<GraphicBuffer*>(buffer);
54}
55
56GraphicBuffer const* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer const* buffer) {
57 return reinterpret_cast<GraphicBuffer const*>(buffer);
58}
59
60AHardwareBuffer* GraphicBuffer::toAHardwareBuffer() {
61 return reinterpret_cast<AHardwareBuffer*>(this);
62}
63
64AHardwareBuffer const* GraphicBuffer::toAHardwareBuffer() const {
65 return reinterpret_cast<AHardwareBuffer const*>(this);
66}
67
Mathias Agopian3330b202009-10-05 17:07:12 -070068GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070069 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070070 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070071{
Dan Stoza01049c82014-11-11 10:32:31 -080072 width =
73 height =
74 stride =
75 format =
Mathias Agopiancb496ac2017-05-22 14:21:00 -070076 usage_deprecated = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070077 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080078 layerCount = 0;
Yi Kong48d76082019-03-24 02:01:06 -070079 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -070080}
81
Chia-I Wub42f1712017-03-21 13:15:39 -070082// deprecated
Dan Stozad3182402014-11-17 12:03:59 -080083GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070084 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopiancb496ac2017-05-22 14:21:00 -070085 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -070086{
Mathias Agopian3330b202009-10-05 17:07:12 -070087}
88
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -080089GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
90 uint32_t inLayerCount, uint64_t inUsage, std::string requestorName)
91 : GraphicBuffer() {
92 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage,
93 std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070094}
95
Chia-I Wub42f1712017-03-21 13:15:39 -070096// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -070097GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
98 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
99 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -0700100 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
Chris Forbes82c04982017-04-19 14:29:54 -0700101 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
Chia-I Wub42f1712017-03-21 13:15:39 -0700102 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700103{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700104}
105
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800106GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method,
107 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
108 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride)
109 : GraphicBuffer() {
110 mInitCheck = initWithHandle(inHandle, method, inWidth, inHeight, inFormat, inLayerCount,
111 inUsage, inStride);
Chia-I Wub42f1712017-03-21 13:15:39 -0700112}
113
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700114#ifndef LIBUI_IN_VNDK
115GraphicBuffer::GraphicBuffer(std::unique_ptr<BufferHubBuffer> buffer) : GraphicBuffer() {
116 if (buffer == nullptr) {
117 mInitCheck = BAD_VALUE;
118 return;
119 }
120
Tianyu Jiang727ede42019-02-01 11:44:51 -0800121 mInitCheck = initWithHandle(buffer->duplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE,
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700122 buffer->desc().width, buffer->desc().height,
123 static_cast<PixelFormat>(buffer->desc().format),
124 buffer->desc().layers, buffer->desc().usage, buffer->desc().stride);
Jiwen 'Steve' Cai29160fb2018-12-13 12:03:28 -0800125 mBufferId = buffer->id();
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700126 mBufferHubBuffer = std::move(buffer);
127}
128#endif // LIBUI_IN_VNDK
129
Mathias Agopian3330b202009-10-05 17:07:12 -0700130GraphicBuffer::~GraphicBuffer()
131{
Alec Mouri6338c9d2019-02-07 16:57:51 -0800132 ATRACE_CALL();
Mathias Agopian3330b202009-10-05 17:07:12 -0700133 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800134 free_handle();
135 }
Marissa Wall78b72202019-03-15 14:58:34 -0700136 for (auto& [callback, context] : mDeathCallbacks) {
137 callback(context, mId);
138 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800139}
140
141void GraphicBuffer::free_handle()
142{
143 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700144 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800145 } else if (mOwner == ownData) {
146 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
147 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700148 }
Yi Kong48d76082019-03-24 02:01:06 -0700149 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700150}
151
152status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800153 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700154}
155
Mathias Agopian678bdd62010-12-03 17:33:09 -0800156void GraphicBuffer::dumpAllocationsToSystemLog()
157{
158 GraphicBufferAllocator::dumpToSystemLog();
159}
160
Iliyan Malchev697526b2011-05-01 11:33:26 -0700161ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700162{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700163 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700164 const_cast<GraphicBuffer*>(this));
165}
166
Dan Stozad3182402014-11-17 12:03:59 -0800167status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700168 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700169{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700170 if (mOwner != ownData)
171 return INVALID_OPERATION;
172
Dan Stozad3182402014-11-17 12:03:59 -0800173 if (handle &&
174 static_cast<int>(inWidth) == width &&
175 static_cast<int>(inHeight) == height &&
176 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700177 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700178 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700179 return NO_ERROR;
180
Mathias Agopian3330b202009-10-05 17:07:12 -0700181 if (handle) {
182 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
183 allocator.free(handle);
Yi Kong48d76082019-03-24 02:01:06 -0700184 handle = nullptr;
Mathias Agopian3330b202009-10-05 17:07:12 -0700185 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700186 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700187}
188
Dan Stoza9de72932015-04-16 17:28:43 -0700189bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700190 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700191{
192 if (static_cast<int>(inWidth) != width) return true;
193 if (static_cast<int>(inHeight) != height) return true;
194 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700195 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700196 if ((usage & inUsage) != inUsage) return true;
Peiyong Lin5d2018a2019-03-26 15:07:54 -0700197 if ((usage & USAGE_PROTECTED) != (inUsage & USAGE_PROTECTED)) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700198 return false;
199}
200
Chia-I Wub42f1712017-03-21 13:15:39 -0700201status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700202 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
203 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700204{
Mathias Agopian3330b202009-10-05 17:07:12 -0700205 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800206 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700207 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700208 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800209 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700210 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700211 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
212
Dan Stozad3182402014-11-17 12:03:59 -0800213 width = static_cast<int>(inWidth);
214 height = static_cast<int>(inHeight);
215 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700216 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700217 usage = inUsage;
218 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800219 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700220 }
221 return err;
222}
223
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800224status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
225 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
226 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) {
227 ANativeWindowBuffer::width = static_cast<int>(inWidth);
228 ANativeWindowBuffer::height = static_cast<int>(inHeight);
229 ANativeWindowBuffer::stride = static_cast<int>(inStride);
230 ANativeWindowBuffer::format = inFormat;
231 ANativeWindowBuffer::usage = inUsage;
232 ANativeWindowBuffer::usage_deprecated = int(inUsage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700233
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800234 ANativeWindowBuffer::layerCount = inLayerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700235
236 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
237
Chia-I Wucb8405e2017-04-17 15:20:19 -0700238 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
239 buffer_handle_t importedHandle;
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800240 status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount,
241 inFormat, inUsage, inStride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700242 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700243 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700244
245 return err;
246 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700247
248 if (method == TAKE_UNREGISTERED_HANDLE) {
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800249 native_handle_close(inHandle);
250 native_handle_delete(const_cast<native_handle_t*>(inHandle));
Chia-I Wucb8405e2017-04-17 15:20:19 -0700251 }
252
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800253 inHandle = importedHandle;
254 mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700255 }
256
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800257 ANativeWindowBuffer::handle = inHandle;
Chia-I Wucb8405e2017-04-17 15:20:19 -0700258
Chia-I Wub42f1712017-03-21 13:15:39 -0700259 return NO_ERROR;
260}
261
Valerie Hau250c6542019-01-31 14:23:43 -0800262status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel,
263 int32_t* outBytesPerStride) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700264 const Rect lockBounds(width, height);
Valerie Hau250c6542019-01-31 14:23:43 -0800265 status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700266 return res;
267}
268
Valerie Hau250c6542019-01-31 14:23:43 -0800269status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr,
270 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800271 if (rect.left < 0 || rect.right > width ||
272 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000273 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800274 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800275 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700276 return BAD_VALUE;
277 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800278
Valerie Hau250c6542019-01-31 14:23:43 -0800279 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr, outBytesPerPixel,
280 outBytesPerStride);
281
Mathias Agopian3330b202009-10-05 17:07:12 -0700282 return res;
283}
284
Dan Stozad3182402014-11-17 12:03:59 -0800285status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700286{
287 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800288 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700289 return res;
290}
291
Dan Stozad3182402014-11-17 12:03:59 -0800292status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
293 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700294{
Dan Stozad3182402014-11-17 12:03:59 -0800295 if (rect.left < 0 || rect.right > width ||
296 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700297 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
298 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800299 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700300 return BAD_VALUE;
301 }
Dan Stozad3182402014-11-17 12:03:59 -0800302 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700303 return res;
304}
305
Mathias Agopian3330b202009-10-05 17:07:12 -0700306status_t GraphicBuffer::unlock()
307{
308 status_t res = getBufferMapper().unlock(handle);
309 return res;
310}
311
Valerie Haub94adfd2019-02-07 14:25:12 -0800312status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd,
313 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Francis Hart8f396012014-04-01 15:30:53 +0300314 const Rect lockBounds(width, height);
Valerie Haub94adfd2019-02-07 14:25:12 -0800315 status_t res =
316 lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Francis Hart8f396012014-04-01 15:30:53 +0300317 return res;
318}
319
Valerie Haub94adfd2019-02-07 14:25:12 -0800320status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd,
321 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
322 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Craig Donnere96a3252017-02-02 12:13:34 -0800323}
324
Valerie Haub94adfd2019-02-07 14:25:12 -0800325status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
326 const Rect& rect, void** vaddr, int fenceFd,
327 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800328 if (rect.left < 0 || rect.right > width ||
329 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300330 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
331 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800332 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300333 return BAD_VALUE;
334 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800335
Valerie Hau0c9fc362019-01-22 09:17:19 -0800336 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect,
Valerie Haub94adfd2019-02-07 14:25:12 -0800337 vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
338
Francis Hart8f396012014-04-01 15:30:53 +0300339 return res;
340}
341
Dan Stozad3182402014-11-17 12:03:59 -0800342status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
343 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300344{
345 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800346 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300347 return res;
348}
349
Dan Stozad3182402014-11-17 12:03:59 -0800350status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
351 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300352{
Dan Stozad3182402014-11-17 12:03:59 -0800353 if (rect.left < 0 || rect.right > width ||
354 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300355 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
356 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800357 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300358 return BAD_VALUE;
359 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700360 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300361 return res;
362}
363
364status_t GraphicBuffer::unlockAsync(int *fenceFd)
365{
366 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
367 return res;
368}
369
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800370status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
371 uint32_t inLayerCount, uint64_t inUsage,
372 bool* outSupported) const {
373 return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage,
374 outSupported);
375}
376
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800377size_t GraphicBuffer::getFlattenedSize() const {
Tianyu Jiang7791e642019-02-15 18:26:17 -0800378#ifndef LIBUI_IN_VNDK
379 if (mBufferHubBuffer != nullptr) {
380 return 48;
381 }
382#endif
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700383 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800384}
Mathias Agopian3330b202009-10-05 17:07:12 -0700385
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800386size_t GraphicBuffer::getFdCount() const {
Tianyu Jiang7791e642019-02-15 18:26:17 -0800387#ifndef LIBUI_IN_VNDK
388 if (mBufferHubBuffer != nullptr) {
389 return 0;
390 }
391#endif
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700392 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800393}
394
Mathias Agopiane1424282013-07-29 21:24:40 -0700395status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Tianyu Jiang7791e642019-02-15 18:26:17 -0800396#ifndef LIBUI_IN_VNDK
397 if (mBufferHubBuffer != nullptr) {
398 return flattenBufferHubBuffer(buffer, size, fds, count);
399 }
400#endif
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800401 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
402 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700403
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800404 size_t fdCountNeeded = GraphicBuffer::getFdCount();
405 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700406
Dan Stozab1363d32014-03-28 15:10:52 -0700407 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700408 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800409 buf[1] = width;
410 buf[2] = height;
411 buf[3] = stride;
412 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700413 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700414 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700415 buf[7] = static_cast<int32_t>(mId >> 32);
416 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
417 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700418 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700419 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700420 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800421
422 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700423 buf[10] = int32_t(mTransportNumFds);
424 buf[11] = int32_t(mTransportNumInts);
425 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700426 memcpy(buf + 13, handle->data + handle->numFds,
Tianyu Jiang7791e642019-02-15 18:26:17 -0800427 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700428 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800429
Dan Stozaeea6d682015-04-20 12:07:13 -0700430 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700431 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700432 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700433 fds += mTransportNumFds;
434 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700435 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800436 return NO_ERROR;
437}
438
Tianyu Jiang7791e642019-02-15 18:26:17 -0800439status_t GraphicBuffer::unflatten(void const*& buffer, size_t& size, int const*& fds,
440 size_t& count) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800441 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700442
443 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
444 // see H2BGraphicBufferProducer.cpp
445 uint32_t flattenWordCount = 0;
446 if (buf[0] == 'GB01') {
447 // new version with 64-bits usage bits
448 flattenWordCount = 13;
449 } else if (buf[0] == 'GBFR') {
450 // old version, when usage bits were 32-bits
451 flattenWordCount = 12;
Tianyu Jiang7791e642019-02-15 18:26:17 -0800452 } else if (buf[0] == 'BHBB') { // BufferHub backed buffer.
453#ifndef LIBUI_IN_VNDK
454 return unflattenBufferHubBuffer(buffer, size, fds, count);
455#else
456 return BAD_TYPE;
457#endif
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700458 } else {
459 return BAD_TYPE;
460 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800461
Tianyu Jiang7791e642019-02-15 18:26:17 -0800462 if (size < 12 * sizeof(int)) {
463 android_errorWriteLog(0x534e4554, "114223584");
464 return NO_MEMORY;
465 }
466
Craig Donner6ebc46a2016-10-21 15:23:44 -0700467 const size_t numFds = static_cast<size_t>(buf[10]);
468 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800469
Michael Lentinec168b8a2015-02-18 10:14:18 -0800470 // Limit the maxNumber to be relatively small. The number of fds or ints
471 // should not come close to this number, and the number itself was simply
472 // chosen to be high enough to not cause issues and low enough to prevent
473 // overflow problems.
474 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700475 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
476 width = height = stride = format = usage_deprecated = 0;
477 layerCount = 0;
478 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700479 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700480 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700481 return BAD_VALUE;
482 }
483
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700484 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800485 if (size < sizeNeeded) return NO_MEMORY;
486
Michael Lentine38803262014-10-31 15:25:03 -0700487 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800488 if (count < fdCountNeeded) return NO_MEMORY;
489
490 if (handle) {
491 // free previous handle if any
492 free_handle();
493 }
494
495 if (numFds || numInts) {
496 width = buf[1];
497 height = buf[2];
498 stride = buf[3];
499 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700500 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700501 usage_deprecated = buf[6];
502 if (flattenWordCount == 13) {
503 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
504 } else {
505 usage = uint64_t(usage_deprecated);
506 }
Tianyu Jiang7791e642019-02-15 18:26:17 -0800507 native_handle* h =
508 native_handle_create(static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700509 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700510 width = height = stride = format = usage_deprecated = 0;
511 layerCount = 0;
512 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700513 handle = nullptr;
Michael Lentine38803262014-10-31 15:25:03 -0700514 ALOGE("unflatten: native_handle_create failed");
515 return NO_MEMORY;
516 }
Dan Stozad3182402014-11-17 12:03:59 -0800517 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700518 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800519 handle = h;
520 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700521 width = height = stride = format = usage_deprecated = 0;
522 layerCount = 0;
523 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700524 handle = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800525 }
526
Craig Donner6ebc46a2016-10-21 15:23:44 -0700527 mId = static_cast<uint64_t>(buf[7]) << 32;
528 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700529
Craig Donner6ebc46a2016-10-21 15:23:44 -0700530 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700531
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800532 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700533
Yi Kong48d76082019-03-24 02:01:06 -0700534 if (handle != nullptr) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700535 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700536 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
537 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700538 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700539 width = height = stride = format = usage_deprecated = 0;
540 layerCount = 0;
541 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700542 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700543 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700544 return err;
545 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700546
547 native_handle_close(handle);
548 native_handle_delete(const_cast<native_handle_t*>(handle));
549 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700550 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700551 }
552
Dan Stozaeea6d682015-04-20 12:07:13 -0700553 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700554 size -= sizeNeeded;
555 fds += numFds;
556 count -= numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800557 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700558}
559
Marissa Wall78b72202019-03-15 14:58:34 -0700560void GraphicBuffer::addDeathCallback(GraphicBufferDeathCallback deathCallback, void* context) {
561 mDeathCallbacks.emplace_back(deathCallback, context);
562}
563
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700564#ifndef LIBUI_IN_VNDK
Tianyu Jiang7791e642019-02-15 18:26:17 -0800565status_t GraphicBuffer::flattenBufferHubBuffer(void*& buffer, size_t& size, int*& fds,
566 size_t& count) const {
567 sp<NativeHandle> tokenHandle = mBufferHubBuffer->duplicate();
568 if (tokenHandle == nullptr || tokenHandle->handle() == nullptr ||
569 tokenHandle->handle()->numFds != 0) {
570 return BAD_VALUE;
571 }
572
573 // Size needed for one label, one number of ints inside the token, one generation number and
574 // the token itself.
575 int numIntsInToken = tokenHandle->handle()->numInts;
576 const size_t sizeNeeded = static_cast<size_t>(3 + numIntsInToken) * sizeof(int);
577 if (size < sizeNeeded) {
578 ALOGE("%s: needed size %d, given size %d. Not enough memory.", __FUNCTION__,
579 static_cast<int>(sizeNeeded), static_cast<int>(size));
580 return NO_MEMORY;
581 }
582 size -= sizeNeeded;
583
584 int* buf = static_cast<int*>(buffer);
585 buf[0] = 'BHBB';
586 buf[1] = numIntsInToken;
587 memcpy(buf + 2, tokenHandle->handle()->data, static_cast<size_t>(numIntsInToken) * sizeof(int));
588 buf[2 + numIntsInToken] = static_cast<int32_t>(mGenerationNumber);
589
590 // Do not pass fds if it is BufferHubBuffer backed GraphicBuffer. Not modifying fds or count.
591 fds += 0;
592 count -= 0;
593 return NO_ERROR;
594}
595
596status_t GraphicBuffer::unflattenBufferHubBuffer(void const*& buffer, size_t& size, int const*& fds,
597 size_t& count) {
598 const int* buf = static_cast<const int*>(buffer);
599 int numIntsInToken = buf[1];
600 // Size needed for one label, one number of ints inside the token, one generation number and
601 // the token itself.
602 const size_t sizeNeeded = static_cast<size_t>(3 + numIntsInToken) * sizeof(int);
603 if (size < sizeNeeded) {
604 ALOGE("%s: needed size %d, given size %d. Not enough memory.", __FUNCTION__,
605 static_cast<int>(sizeNeeded), static_cast<int>(size));
606 return NO_MEMORY;
607 }
608 size -= sizeNeeded;
609 native_handle_t* importToken = native_handle_create(/*numFds=*/0, /*numInts=*/numIntsInToken);
610 memcpy(importToken->data, buf + 2, static_cast<size_t>(buf[1]) * sizeof(int));
611 sp<NativeHandle> importTokenHandle = NativeHandle::create(importToken, /*ownHandle=*/true);
612 std::unique_ptr<BufferHubBuffer> bufferHubBuffer = BufferHubBuffer::import(importTokenHandle);
613 if (bufferHubBuffer == nullptr || bufferHubBuffer.get() == nullptr) {
614 return BAD_VALUE;
615 }
616 // Reconstruct this GraphicBuffer object using the new BufferHubBuffer object.
617 if (handle) {
618 free_handle();
619 }
620 mId = 0;
621 mGenerationNumber = static_cast<uint32_t>(buf[2 + numIntsInToken]);
622 mInitCheck =
623 initWithHandle(bufferHubBuffer->duplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE,
624 bufferHubBuffer->desc().width, bufferHubBuffer->desc().height,
625 static_cast<PixelFormat>(bufferHubBuffer->desc().format),
626 bufferHubBuffer->desc().layers, bufferHubBuffer->desc().usage,
627 bufferHubBuffer->desc().stride);
628 mBufferId = bufferHubBuffer->id();
629 mBufferHubBuffer.reset(std::move(bufferHubBuffer.get()));
630
631 // BufferHubBuffer backed GraphicBuffer does not have flattened handle. Not modifying fds or
632 // count.
633 fds += 0;
634 count -= 0;
635 return NO_ERROR;
636}
637
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700638bool GraphicBuffer::isBufferHubBuffer() const {
639 return mBufferHubBuffer != nullptr;
640}
641#endif // LIBUI_IN_VNDK
642
Mathias Agopian3330b202009-10-05 17:07:12 -0700643// ---------------------------------------------------------------------------
644
645}; // namespace android