blob: 2fb1099f968ba1bd5debd65a3ec1bd37cae1655d [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;
Dan Stoza9de72932015-04-16 17:28:43 -0700197 return false;
198}
199
Chia-I Wub42f1712017-03-21 13:15:39 -0700200status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700201 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
202 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700203{
Mathias Agopian3330b202009-10-05 17:07:12 -0700204 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800205 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700206 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700207 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800208 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700209 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700210 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
211
Dan Stozad3182402014-11-17 12:03:59 -0800212 width = static_cast<int>(inWidth);
213 height = static_cast<int>(inHeight);
214 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700215 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700216 usage = inUsage;
217 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800218 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700219 }
220 return err;
221}
222
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800223status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
224 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
225 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) {
226 ANativeWindowBuffer::width = static_cast<int>(inWidth);
227 ANativeWindowBuffer::height = static_cast<int>(inHeight);
228 ANativeWindowBuffer::stride = static_cast<int>(inStride);
229 ANativeWindowBuffer::format = inFormat;
230 ANativeWindowBuffer::usage = inUsage;
231 ANativeWindowBuffer::usage_deprecated = int(inUsage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700232
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800233 ANativeWindowBuffer::layerCount = inLayerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700234
235 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
236
Chia-I Wucb8405e2017-04-17 15:20:19 -0700237 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
238 buffer_handle_t importedHandle;
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800239 status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount,
240 inFormat, inUsage, inStride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700241 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700242 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700243
244 return err;
245 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700246
247 if (method == TAKE_UNREGISTERED_HANDLE) {
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800248 native_handle_close(inHandle);
249 native_handle_delete(const_cast<native_handle_t*>(inHandle));
Chia-I Wucb8405e2017-04-17 15:20:19 -0700250 }
251
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800252 inHandle = importedHandle;
253 mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700254 }
255
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800256 ANativeWindowBuffer::handle = inHandle;
Chia-I Wucb8405e2017-04-17 15:20:19 -0700257
Chia-I Wub42f1712017-03-21 13:15:39 -0700258 return NO_ERROR;
259}
260
Valerie Hau250c6542019-01-31 14:23:43 -0800261status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel,
262 int32_t* outBytesPerStride) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700263 const Rect lockBounds(width, height);
Valerie Hau250c6542019-01-31 14:23:43 -0800264 status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700265 return res;
266}
267
Valerie Hau250c6542019-01-31 14:23:43 -0800268status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr,
269 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800270 if (rect.left < 0 || rect.right > width ||
271 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000272 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800273 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800274 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700275 return BAD_VALUE;
276 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800277
Valerie Hau250c6542019-01-31 14:23:43 -0800278 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr, outBytesPerPixel,
279 outBytesPerStride);
280
Mathias Agopian3330b202009-10-05 17:07:12 -0700281 return res;
282}
283
Dan Stozad3182402014-11-17 12:03:59 -0800284status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700285{
286 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800287 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700288 return res;
289}
290
Dan Stozad3182402014-11-17 12:03:59 -0800291status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
292 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700293{
Dan Stozad3182402014-11-17 12:03:59 -0800294 if (rect.left < 0 || rect.right > width ||
295 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700296 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
297 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800298 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700299 return BAD_VALUE;
300 }
Dan Stozad3182402014-11-17 12:03:59 -0800301 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700302 return res;
303}
304
Mathias Agopian3330b202009-10-05 17:07:12 -0700305status_t GraphicBuffer::unlock()
306{
307 status_t res = getBufferMapper().unlock(handle);
308 return res;
309}
310
Valerie Haub94adfd2019-02-07 14:25:12 -0800311status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd,
312 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Francis Hart8f396012014-04-01 15:30:53 +0300313 const Rect lockBounds(width, height);
Valerie Haub94adfd2019-02-07 14:25:12 -0800314 status_t res =
315 lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Francis Hart8f396012014-04-01 15:30:53 +0300316 return res;
317}
318
Valerie Haub94adfd2019-02-07 14:25:12 -0800319status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd,
320 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
321 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Craig Donnere96a3252017-02-02 12:13:34 -0800322}
323
Valerie Haub94adfd2019-02-07 14:25:12 -0800324status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
325 const Rect& rect, void** vaddr, int fenceFd,
326 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800327 if (rect.left < 0 || rect.right > width ||
328 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300329 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
330 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800331 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300332 return BAD_VALUE;
333 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800334
Valerie Hau0c9fc362019-01-22 09:17:19 -0800335 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect,
Valerie Haub94adfd2019-02-07 14:25:12 -0800336 vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
337
Francis Hart8f396012014-04-01 15:30:53 +0300338 return res;
339}
340
Dan Stozad3182402014-11-17 12:03:59 -0800341status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
342 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300343{
344 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800345 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300346 return res;
347}
348
Dan Stozad3182402014-11-17 12:03:59 -0800349status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
350 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300351{
Dan Stozad3182402014-11-17 12:03:59 -0800352 if (rect.left < 0 || rect.right > width ||
353 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300354 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
355 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800356 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300357 return BAD_VALUE;
358 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700359 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300360 return res;
361}
362
363status_t GraphicBuffer::unlockAsync(int *fenceFd)
364{
365 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
366 return res;
367}
368
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800369status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
370 uint32_t inLayerCount, uint64_t inUsage,
371 bool* outSupported) const {
372 return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage,
373 outSupported);
374}
375
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800376size_t GraphicBuffer::getFlattenedSize() const {
Tianyu Jiang7791e642019-02-15 18:26:17 -0800377#ifndef LIBUI_IN_VNDK
378 if (mBufferHubBuffer != nullptr) {
379 return 48;
380 }
381#endif
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700382 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800383}
Mathias Agopian3330b202009-10-05 17:07:12 -0700384
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800385size_t GraphicBuffer::getFdCount() const {
Tianyu Jiang7791e642019-02-15 18:26:17 -0800386#ifndef LIBUI_IN_VNDK
387 if (mBufferHubBuffer != nullptr) {
388 return 0;
389 }
390#endif
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700391 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800392}
393
Mathias Agopiane1424282013-07-29 21:24:40 -0700394status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Tianyu Jiang7791e642019-02-15 18:26:17 -0800395#ifndef LIBUI_IN_VNDK
396 if (mBufferHubBuffer != nullptr) {
Tianyu Jiang69823cf2019-03-25 15:38:17 -0700397 return flattenBufferHubBuffer(buffer, size);
Tianyu Jiang7791e642019-02-15 18:26:17 -0800398 }
399#endif
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800400 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
401 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700402
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800403 size_t fdCountNeeded = GraphicBuffer::getFdCount();
404 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700405
Dan Stozab1363d32014-03-28 15:10:52 -0700406 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700407 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800408 buf[1] = width;
409 buf[2] = height;
410 buf[3] = stride;
411 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700412 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700413 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700414 buf[7] = static_cast<int32_t>(mId >> 32);
415 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
416 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700417 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700418 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700419 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800420
421 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700422 buf[10] = int32_t(mTransportNumFds);
423 buf[11] = int32_t(mTransportNumInts);
424 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700425 memcpy(buf + 13, handle->data + handle->numFds,
Tianyu Jiang7791e642019-02-15 18:26:17 -0800426 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700427 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800428
Dan Stozaeea6d682015-04-20 12:07:13 -0700429 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700430 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700431 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700432 fds += mTransportNumFds;
433 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700434 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800435 return NO_ERROR;
436}
437
Tianyu Jiang7791e642019-02-15 18:26:17 -0800438status_t GraphicBuffer::unflatten(void const*& buffer, size_t& size, int const*& fds,
439 size_t& count) {
Tianyu Jiang69823cf2019-03-25 15:38:17 -0700440 // Check if size is not smaller than buf[0] is supposed to take.
441 if (size < sizeof(int)) {
442 return NO_MEMORY;
443 }
444
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800445 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700446
447 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
448 // see H2BGraphicBufferProducer.cpp
449 uint32_t flattenWordCount = 0;
450 if (buf[0] == 'GB01') {
451 // new version with 64-bits usage bits
452 flattenWordCount = 13;
453 } else if (buf[0] == 'GBFR') {
454 // old version, when usage bits were 32-bits
455 flattenWordCount = 12;
Tianyu Jiang7791e642019-02-15 18:26:17 -0800456 } else if (buf[0] == 'BHBB') { // BufferHub backed buffer.
457#ifndef LIBUI_IN_VNDK
Tianyu Jiang69823cf2019-03-25 15:38:17 -0700458 return unflattenBufferHubBuffer(buffer, size);
Tianyu Jiang7791e642019-02-15 18:26:17 -0800459#else
460 return BAD_TYPE;
461#endif
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700462 } else {
463 return BAD_TYPE;
464 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800465
Tianyu Jiang7791e642019-02-15 18:26:17 -0800466 if (size < 12 * sizeof(int)) {
467 android_errorWriteLog(0x534e4554, "114223584");
468 return NO_MEMORY;
469 }
470
Craig Donner6ebc46a2016-10-21 15:23:44 -0700471 const size_t numFds = static_cast<size_t>(buf[10]);
472 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800473
Michael Lentinec168b8a2015-02-18 10:14:18 -0800474 // Limit the maxNumber to be relatively small. The number of fds or ints
475 // should not come close to this number, and the number itself was simply
476 // chosen to be high enough to not cause issues and low enough to prevent
477 // overflow problems.
478 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700479 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
480 width = height = stride = format = usage_deprecated = 0;
481 layerCount = 0;
482 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700483 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700484 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700485 return BAD_VALUE;
486 }
487
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700488 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800489 if (size < sizeNeeded) return NO_MEMORY;
490
Michael Lentine38803262014-10-31 15:25:03 -0700491 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800492 if (count < fdCountNeeded) return NO_MEMORY;
493
494 if (handle) {
495 // free previous handle if any
496 free_handle();
497 }
498
499 if (numFds || numInts) {
500 width = buf[1];
501 height = buf[2];
502 stride = buf[3];
503 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700504 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700505 usage_deprecated = buf[6];
506 if (flattenWordCount == 13) {
507 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
508 } else {
509 usage = uint64_t(usage_deprecated);
510 }
Tianyu Jiang7791e642019-02-15 18:26:17 -0800511 native_handle* h =
512 native_handle_create(static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700513 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700514 width = height = stride = format = usage_deprecated = 0;
515 layerCount = 0;
516 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700517 handle = nullptr;
Michael Lentine38803262014-10-31 15:25:03 -0700518 ALOGE("unflatten: native_handle_create failed");
519 return NO_MEMORY;
520 }
Dan Stozad3182402014-11-17 12:03:59 -0800521 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700522 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800523 handle = h;
524 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700525 width = height = stride = format = usage_deprecated = 0;
526 layerCount = 0;
527 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700528 handle = nullptr;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800529 }
530
Craig Donner6ebc46a2016-10-21 15:23:44 -0700531 mId = static_cast<uint64_t>(buf[7]) << 32;
532 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700533
Craig Donner6ebc46a2016-10-21 15:23:44 -0700534 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700535
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800536 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700537
Yi Kong48d76082019-03-24 02:01:06 -0700538 if (handle != nullptr) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700539 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700540 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
541 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700542 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700543 width = height = stride = format = usage_deprecated = 0;
544 layerCount = 0;
545 usage = 0;
Yi Kong48d76082019-03-24 02:01:06 -0700546 handle = nullptr;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700547 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700548 return err;
549 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700550
551 native_handle_close(handle);
552 native_handle_delete(const_cast<native_handle_t*>(handle));
553 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700554 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700555 }
556
Dan Stozaeea6d682015-04-20 12:07:13 -0700557 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700558 size -= sizeNeeded;
559 fds += numFds;
560 count -= numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800561 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700562}
563
Marissa Wall78b72202019-03-15 14:58:34 -0700564void GraphicBuffer::addDeathCallback(GraphicBufferDeathCallback deathCallback, void* context) {
565 mDeathCallbacks.emplace_back(deathCallback, context);
566}
567
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700568#ifndef LIBUI_IN_VNDK
Tianyu Jiang69823cf2019-03-25 15:38:17 -0700569status_t GraphicBuffer::flattenBufferHubBuffer(void*& buffer, size_t& size) const {
Tianyu Jiang7791e642019-02-15 18:26:17 -0800570 sp<NativeHandle> tokenHandle = mBufferHubBuffer->duplicate();
571 if (tokenHandle == nullptr || tokenHandle->handle() == nullptr ||
572 tokenHandle->handle()->numFds != 0) {
573 return BAD_VALUE;
574 }
575
576 // Size needed for one label, one number of ints inside the token, one generation number and
577 // the token itself.
578 int numIntsInToken = tokenHandle->handle()->numInts;
579 const size_t sizeNeeded = static_cast<size_t>(3 + numIntsInToken) * sizeof(int);
580 if (size < sizeNeeded) {
581 ALOGE("%s: needed size %d, given size %d. Not enough memory.", __FUNCTION__,
582 static_cast<int>(sizeNeeded), static_cast<int>(size));
583 return NO_MEMORY;
584 }
585 size -= sizeNeeded;
586
587 int* buf = static_cast<int*>(buffer);
588 buf[0] = 'BHBB';
589 buf[1] = numIntsInToken;
590 memcpy(buf + 2, tokenHandle->handle()->data, static_cast<size_t>(numIntsInToken) * sizeof(int));
591 buf[2 + numIntsInToken] = static_cast<int32_t>(mGenerationNumber);
592
Tianyu Jiang7791e642019-02-15 18:26:17 -0800593 return NO_ERROR;
594}
595
Tianyu Jiang69823cf2019-03-25 15:38:17 -0700596status_t GraphicBuffer::unflattenBufferHubBuffer(void const*& buffer, size_t& size) {
Tianyu Jiang7791e642019-02-15 18:26:17 -0800597 const int* buf = static_cast<const int*>(buffer);
598 int numIntsInToken = buf[1];
599 // Size needed for one label, one number of ints inside the token, one generation number and
600 // the token itself.
601 const size_t sizeNeeded = static_cast<size_t>(3 + numIntsInToken) * sizeof(int);
602 if (size < sizeNeeded) {
603 ALOGE("%s: needed size %d, given size %d. Not enough memory.", __FUNCTION__,
604 static_cast<int>(sizeNeeded), static_cast<int>(size));
605 return NO_MEMORY;
606 }
607 size -= sizeNeeded;
608 native_handle_t* importToken = native_handle_create(/*numFds=*/0, /*numInts=*/numIntsInToken);
609 memcpy(importToken->data, buf + 2, static_cast<size_t>(buf[1]) * sizeof(int));
610 sp<NativeHandle> importTokenHandle = NativeHandle::create(importToken, /*ownHandle=*/true);
611 std::unique_ptr<BufferHubBuffer> bufferHubBuffer = BufferHubBuffer::import(importTokenHandle);
612 if (bufferHubBuffer == nullptr || bufferHubBuffer.get() == nullptr) {
613 return BAD_VALUE;
614 }
615 // Reconstruct this GraphicBuffer object using the new BufferHubBuffer object.
616 if (handle) {
617 free_handle();
618 }
619 mId = 0;
620 mGenerationNumber = static_cast<uint32_t>(buf[2 + numIntsInToken]);
621 mInitCheck =
622 initWithHandle(bufferHubBuffer->duplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE,
623 bufferHubBuffer->desc().width, bufferHubBuffer->desc().height,
624 static_cast<PixelFormat>(bufferHubBuffer->desc().format),
625 bufferHubBuffer->desc().layers, bufferHubBuffer->desc().usage,
626 bufferHubBuffer->desc().stride);
627 mBufferId = bufferHubBuffer->id();
628 mBufferHubBuffer.reset(std::move(bufferHubBuffer.get()));
629
Tianyu Jiang7791e642019-02-15 18:26:17 -0800630 return NO_ERROR;
631}
632
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700633bool GraphicBuffer::isBufferHubBuffer() const {
634 return mBufferHubBuffer != nullptr;
635}
636#endif // LIBUI_IN_VNDK
637
Mathias Agopian3330b202009-10-05 17:07:12 -0700638// ---------------------------------------------------------------------------
639
640}; // namespace android