blob: e606e26f8bb148a72a307b120422356642c6322f [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"
18
Mathias Agopian3330b202009-10-05 17:07:12 -070019#include <ui/GraphicBuffer.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080020
21#include <cutils/atomic.h>
22
Jesse Hall79927812017-03-23 11:03:23 -070023#include <grallocusage/GrallocUsageConversion.h>
24
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -070025#ifndef LIBUI_IN_VNDK
26#include <ui/BufferHubBuffer.h>
27#endif // LIBUI_IN_VNDK
28
Chia-I Wu5bac7f32017-04-06 12:34:32 -070029#include <ui/Gralloc2.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070030#include <ui/GraphicBufferAllocator.h>
31#include <ui/GraphicBufferMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070032
Mathias Agopian3330b202009-10-05 17:07:12 -070033namespace android {
34
35// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070036// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070037// ===========================================================================
38
Dan Stozab1363d32014-03-28 15:10:52 -070039static uint64_t getUniqueId() {
40 static volatile int32_t nextId = 0;
41 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
42 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
43 return id;
44}
45
Mathias Agopianf543e5a2017-04-03 17:16:41 -070046sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
47 return static_cast<GraphicBuffer *>(anwb);
48}
49
Mathias Agopian3330b202009-10-05 17:07:12 -070050GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070051 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070052 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070053{
Dan Stoza01049c82014-11-11 10:32:31 -080054 width =
55 height =
56 stride =
57 format =
Mathias Agopiancb496ac2017-05-22 14:21:00 -070058 usage_deprecated = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070059 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080060 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070061 handle = NULL;
62}
63
Chia-I Wub42f1712017-03-21 13:15:39 -070064// deprecated
Dan Stozad3182402014-11-17 12:03:59 -080065GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070066 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopiancb496ac2017-05-22 14:21:00 -070067 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -070068{
Mathias Agopian3330b202009-10-05 17:07:12 -070069}
70
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -080071GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
72 uint32_t inLayerCount, uint64_t inUsage, std::string requestorName)
73 : GraphicBuffer() {
74 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage,
75 std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070076}
77
Chia-I Wub42f1712017-03-21 13:15:39 -070078// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -070079GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
80 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
81 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -070082 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
Chris Forbes82c04982017-04-19 14:29:54 -070083 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
Chia-I Wub42f1712017-03-21 13:15:39 -070084 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070085{
Mathias Agopian54ba51d2009-10-26 20:12:37 -070086}
87
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -080088GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method,
89 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
90 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride)
91 : GraphicBuffer() {
92 mInitCheck = initWithHandle(inHandle, method, inWidth, inHeight, inFormat, inLayerCount,
93 inUsage, inStride);
Chia-I Wub42f1712017-03-21 13:15:39 -070094}
95
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -070096#ifndef LIBUI_IN_VNDK
97GraphicBuffer::GraphicBuffer(std::unique_ptr<BufferHubBuffer> buffer) : GraphicBuffer() {
98 if (buffer == nullptr) {
99 mInitCheck = BAD_VALUE;
100 return;
101 }
102
103 mInitCheck = initWithHandle(buffer->DuplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE,
104 buffer->desc().width, buffer->desc().height,
105 static_cast<PixelFormat>(buffer->desc().format),
106 buffer->desc().layers, buffer->desc().usage, buffer->desc().stride);
107 mBufferHubBuffer = std::move(buffer);
108}
109#endif // LIBUI_IN_VNDK
110
Mathias Agopian3330b202009-10-05 17:07:12 -0700111GraphicBuffer::~GraphicBuffer()
112{
113 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800114 free_handle();
115 }
116}
117
118void GraphicBuffer::free_handle()
119{
120 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700121 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800122 } else if (mOwner == ownData) {
123 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
124 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700125 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700126 handle = NULL;
Mathias Agopian3330b202009-10-05 17:07:12 -0700127}
128
129status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800130 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700131}
132
Mathias Agopian678bdd62010-12-03 17:33:09 -0800133void GraphicBuffer::dumpAllocationsToSystemLog()
134{
135 GraphicBufferAllocator::dumpToSystemLog();
136}
137
Iliyan Malchev697526b2011-05-01 11:33:26 -0700138ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700139{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700140 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700141 const_cast<GraphicBuffer*>(this));
142}
143
Dan Stozad3182402014-11-17 12:03:59 -0800144status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700145 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700146{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700147 if (mOwner != ownData)
148 return INVALID_OPERATION;
149
Dan Stozad3182402014-11-17 12:03:59 -0800150 if (handle &&
151 static_cast<int>(inWidth) == width &&
152 static_cast<int>(inHeight) == height &&
153 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700154 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700155 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700156 return NO_ERROR;
157
Mathias Agopian3330b202009-10-05 17:07:12 -0700158 if (handle) {
159 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
160 allocator.free(handle);
161 handle = 0;
162 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700163 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700164}
165
Dan Stoza9de72932015-04-16 17:28:43 -0700166bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700167 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700168{
169 if (static_cast<int>(inWidth) != width) return true;
170 if (static_cast<int>(inHeight) != height) return true;
171 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700172 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700173 if ((usage & inUsage) != inUsage) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700174 return false;
175}
176
Chia-I Wub42f1712017-03-21 13:15:39 -0700177status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700178 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
179 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700180{
Mathias Agopian3330b202009-10-05 17:07:12 -0700181 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800182 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700183 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700184 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800185 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700186 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700187 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
188
Dan Stozad3182402014-11-17 12:03:59 -0800189 width = static_cast<int>(inWidth);
190 height = static_cast<int>(inHeight);
191 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700192 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700193 usage = inUsage;
194 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800195 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700196 }
197 return err;
198}
199
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800200status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
201 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
202 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) {
203 ANativeWindowBuffer::width = static_cast<int>(inWidth);
204 ANativeWindowBuffer::height = static_cast<int>(inHeight);
205 ANativeWindowBuffer::stride = static_cast<int>(inStride);
206 ANativeWindowBuffer::format = inFormat;
207 ANativeWindowBuffer::usage = inUsage;
208 ANativeWindowBuffer::usage_deprecated = int(inUsage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700209
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800210 ANativeWindowBuffer::layerCount = inLayerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700211
212 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
213
Chia-I Wucb8405e2017-04-17 15:20:19 -0700214 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
215 buffer_handle_t importedHandle;
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800216 status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount,
217 inFormat, inUsage, inStride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700218 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700219 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700220
221 return err;
222 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700223
224 if (method == TAKE_UNREGISTERED_HANDLE) {
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800225 native_handle_close(inHandle);
226 native_handle_delete(const_cast<native_handle_t*>(inHandle));
Chia-I Wucb8405e2017-04-17 15:20:19 -0700227 }
228
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800229 inHandle = importedHandle;
230 mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700231 }
232
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800233 ANativeWindowBuffer::handle = inHandle;
Chia-I Wucb8405e2017-04-17 15:20:19 -0700234
Chia-I Wub42f1712017-03-21 13:15:39 -0700235 return NO_ERROR;
236}
237
Dan Stozad3182402014-11-17 12:03:59 -0800238status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700239{
240 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800241 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700242 return res;
243}
244
Dan Stozad3182402014-11-17 12:03:59 -0800245status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700246{
Dan Stozad3182402014-11-17 12:03:59 -0800247 if (rect.left < 0 || rect.right > width ||
248 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000249 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800250 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800251 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700252 return BAD_VALUE;
253 }
Dan Stozad3182402014-11-17 12:03:59 -0800254 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700255 return res;
256}
257
Dan Stozad3182402014-11-17 12:03:59 -0800258status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700259{
260 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800261 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700262 return res;
263}
264
Dan Stozad3182402014-11-17 12:03:59 -0800265status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
266 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700267{
Dan Stozad3182402014-11-17 12:03:59 -0800268 if (rect.left < 0 || rect.right > width ||
269 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700270 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
271 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800272 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700273 return BAD_VALUE;
274 }
Dan Stozad3182402014-11-17 12:03:59 -0800275 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700276 return res;
277}
278
Mathias Agopian3330b202009-10-05 17:07:12 -0700279status_t GraphicBuffer::unlock()
280{
281 status_t res = getBufferMapper().unlock(handle);
282 return res;
283}
284
Dan Stozad3182402014-11-17 12:03:59 -0800285status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300286{
287 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800288 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300289 return res;
290}
291
Dan Stozad3182402014-11-17 12:03:59 -0800292status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
293 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300294{
Craig Donnere96a3252017-02-02 12:13:34 -0800295 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
296}
297
298status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
299 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
300{
Dan Stozad3182402014-11-17 12:03:59 -0800301 if (rect.left < 0 || rect.right > width ||
302 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300303 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
304 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800305 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300306 return BAD_VALUE;
307 }
Craig Donnere96a3252017-02-02 12:13:34 -0800308 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
309 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300310 return res;
311}
312
Dan Stozad3182402014-11-17 12:03:59 -0800313status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
314 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300315{
316 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800317 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
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, const Rect& rect,
322 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300323{
Dan Stozad3182402014-11-17 12:03:59 -0800324 if (rect.left < 0 || rect.right > width ||
325 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300326 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
327 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800328 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300329 return BAD_VALUE;
330 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700331 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300332 return res;
333}
334
335status_t GraphicBuffer::unlockAsync(int *fenceFd)
336{
337 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
338 return res;
339}
340
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800341size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700342 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800343}
Mathias Agopian3330b202009-10-05 17:07:12 -0700344
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800345size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700346 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800347}
348
Mathias Agopiane1424282013-07-29 21:24:40 -0700349status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800350 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
351 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700352
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800353 size_t fdCountNeeded = GraphicBuffer::getFdCount();
354 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700355
Dan Stozab1363d32014-03-28 15:10:52 -0700356 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700357 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800358 buf[1] = width;
359 buf[2] = height;
360 buf[3] = stride;
361 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700362 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700363 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700364 buf[7] = static_cast<int32_t>(mId >> 32);
365 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
366 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700367 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700368 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700369 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800370
371 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700372 buf[10] = int32_t(mTransportNumFds);
373 buf[11] = int32_t(mTransportNumInts);
374 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700375 memcpy(buf + 13, handle->data + handle->numFds,
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700376 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700377 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800378
Dan Stozaeea6d682015-04-20 12:07:13 -0700379 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700380 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700381 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700382 fds += mTransportNumFds;
383 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700384 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700385
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800386 return NO_ERROR;
387}
388
Mathias Agopiane1424282013-07-29 21:24:40 -0700389status_t GraphicBuffer::unflatten(
390 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Chia-I Wubf8d7212018-10-09 15:22:46 -0700391 if (size < 12 * sizeof(int)) {
392 android_errorWriteLog(0x534e4554, "114223584");
393 return NO_MEMORY;
394 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800395
396 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700397
398 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
399 // see H2BGraphicBufferProducer.cpp
400 uint32_t flattenWordCount = 0;
401 if (buf[0] == 'GB01') {
402 // new version with 64-bits usage bits
403 flattenWordCount = 13;
404 } else if (buf[0] == 'GBFR') {
405 // old version, when usage bits were 32-bits
406 flattenWordCount = 12;
407 } else {
408 return BAD_TYPE;
409 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800410
Craig Donner6ebc46a2016-10-21 15:23:44 -0700411 const size_t numFds = static_cast<size_t>(buf[10]);
412 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800413
Michael Lentinec168b8a2015-02-18 10:14:18 -0800414 // Limit the maxNumber to be relatively small. The number of fds or ints
415 // should not come close to this number, and the number itself was simply
416 // chosen to be high enough to not cause issues and low enough to prevent
417 // overflow problems.
418 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700419 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
420 width = height = stride = format = usage_deprecated = 0;
421 layerCount = 0;
422 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700423 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700424 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700425 return BAD_VALUE;
426 }
427
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700428 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800429 if (size < sizeNeeded) return NO_MEMORY;
430
Michael Lentine38803262014-10-31 15:25:03 -0700431 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800432 if (count < fdCountNeeded) return NO_MEMORY;
433
434 if (handle) {
435 // free previous handle if any
436 free_handle();
437 }
438
439 if (numFds || numInts) {
440 width = buf[1];
441 height = buf[2];
442 stride = buf[3];
443 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700444 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700445 usage_deprecated = buf[6];
446 if (flattenWordCount == 13) {
447 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
448 } else {
449 usage = uint64_t(usage_deprecated);
450 }
Dan Stozad3182402014-11-17 12:03:59 -0800451 native_handle* h = native_handle_create(
452 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700453 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700454 width = height = stride = format = usage_deprecated = 0;
455 layerCount = 0;
456 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700457 handle = NULL;
458 ALOGE("unflatten: native_handle_create failed");
459 return NO_MEMORY;
460 }
Dan Stozad3182402014-11-17 12:03:59 -0800461 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700462 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800463 handle = h;
464 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700465 width = height = stride = format = usage_deprecated = 0;
466 layerCount = 0;
467 usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800468 handle = NULL;
469 }
470
Craig Donner6ebc46a2016-10-21 15:23:44 -0700471 mId = static_cast<uint64_t>(buf[7]) << 32;
472 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700473
Craig Donner6ebc46a2016-10-21 15:23:44 -0700474 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700475
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800476 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700477
478 if (handle != 0) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700479 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700480 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
481 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700482 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700483 width = height = stride = format = usage_deprecated = 0;
484 layerCount = 0;
485 usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800486 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700487 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700488 return err;
489 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700490
491 native_handle_close(handle);
492 native_handle_delete(const_cast<native_handle_t*>(handle));
493 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700494 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700495 }
496
Dan Stozaeea6d682015-04-20 12:07:13 -0700497 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700498 size -= sizeNeeded;
499 fds += numFds;
500 count -= numFds;
501
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800502 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700503}
504
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700505#ifndef LIBUI_IN_VNDK
506bool GraphicBuffer::isBufferHubBuffer() const {
507 return mBufferHubBuffer != nullptr;
508}
509#endif // LIBUI_IN_VNDK
510
Mathias Agopian3330b202009-10-05 17:07:12 -0700511// ---------------------------------------------------------------------------
512
513}; // namespace android