blob: 4ed2aa42fcf8c907fb58886f129acedd90e3e758 [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
Chia-I Wu5bac7f32017-04-06 12:34:32 -070025#include <ui/Gralloc2.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070026#include <ui/GraphicBufferAllocator.h>
27#include <ui/GraphicBufferMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070028
Mathias Agopian3330b202009-10-05 17:07:12 -070029namespace android {
30
31// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070032// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070033// ===========================================================================
34
Dan Stozab1363d32014-03-28 15:10:52 -070035static uint64_t getUniqueId() {
36 static volatile int32_t nextId = 0;
37 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
38 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
39 return id;
40}
41
Mathias Agopianf543e5a2017-04-03 17:16:41 -070042sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
43 return static_cast<GraphicBuffer *>(anwb);
44}
45
Mathias Agopian3330b202009-10-05 17:07:12 -070046GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070047 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070048 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070049{
Dan Stoza01049c82014-11-11 10:32:31 -080050 width =
51 height =
52 stride =
53 format =
Mathias Agopiancb496ac2017-05-22 14:21:00 -070054 usage_deprecated = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070055 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080056 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070057 handle = NULL;
58}
59
Chia-I Wub42f1712017-03-21 13:15:39 -070060// deprecated
Dan Stozad3182402014-11-17 12:03:59 -080061GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070062 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopiancb496ac2017-05-22 14:21:00 -070063 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -070064{
Mathias Agopian3330b202009-10-05 17:07:12 -070065}
66
Dan Stozad3182402014-11-17 12:03:59 -080067GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Mathias Agopiancb496ac2017-05-22 14:21:00 -070068 PixelFormat inFormat, uint32_t inLayerCount, uint64_t usage, std::string requestorName)
Chia-I Wub42f1712017-03-21 13:15:39 -070069 : GraphicBuffer()
Craig Donner6ebc46a2016-10-21 15:23:44 -070070{
Chia-I Wub42f1712017-03-21 13:15:39 -070071 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -070072 usage, std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070073}
74
Chia-I Wub42f1712017-03-21 13:15:39 -070075// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -070076GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
77 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
78 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -070079 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
Chris Forbes82c04982017-04-19 14:29:54 -070080 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
Chia-I Wub42f1712017-03-21 13:15:39 -070081 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070082{
Mathias Agopian54ba51d2009-10-26 20:12:37 -070083}
84
Chia-I Wub42f1712017-03-21 13:15:39 -070085GraphicBuffer::GraphicBuffer(const native_handle_t* handle,
86 HandleWrapMethod method, uint32_t width, uint32_t height,
87 PixelFormat format, uint32_t layerCount,
Chris Forbes82c04982017-04-19 14:29:54 -070088 uint64_t usage,
Chia-I Wub42f1712017-03-21 13:15:39 -070089 uint32_t stride)
90 : GraphicBuffer()
91{
92 mInitCheck = initWithHandle(handle, method, width, height, format,
Chris Forbes82c04982017-04-19 14:29:54 -070093 layerCount, usage, stride);
Chia-I Wub42f1712017-03-21 13:15:39 -070094}
95
Mathias Agopian3330b202009-10-05 17:07:12 -070096GraphicBuffer::~GraphicBuffer()
97{
98 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -080099 free_handle();
100 }
101}
102
103void GraphicBuffer::free_handle()
104{
105 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700106 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800107 } else if (mOwner == ownData) {
108 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
109 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700110 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700111 handle = NULL;
Mathias Agopian3330b202009-10-05 17:07:12 -0700112}
113
114status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800115 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700116}
117
Mathias Agopian678bdd62010-12-03 17:33:09 -0800118void GraphicBuffer::dumpAllocationsToSystemLog()
119{
120 GraphicBufferAllocator::dumpToSystemLog();
121}
122
Iliyan Malchev697526b2011-05-01 11:33:26 -0700123ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700124{
Colin Cross18fae752014-07-22 15:55:08 -0700125 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700126 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700127 const_cast<GraphicBuffer*>(this));
128}
129
Dan Stozad3182402014-11-17 12:03:59 -0800130status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700131 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700132{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700133 if (mOwner != ownData)
134 return INVALID_OPERATION;
135
Dan Stozad3182402014-11-17 12:03:59 -0800136 if (handle &&
137 static_cast<int>(inWidth) == width &&
138 static_cast<int>(inHeight) == height &&
139 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700140 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700141 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700142 return NO_ERROR;
143
Mathias Agopian3330b202009-10-05 17:07:12 -0700144 if (handle) {
145 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
146 allocator.free(handle);
147 handle = 0;
148 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700149 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700150}
151
Dan Stoza9de72932015-04-16 17:28:43 -0700152bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700153 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700154{
155 if (static_cast<int>(inWidth) != width) return true;
156 if (static_cast<int>(inHeight) != height) return true;
157 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700158 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700159 if ((usage & inUsage) != inUsage) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700160 return false;
161}
162
Chia-I Wub42f1712017-03-21 13:15:39 -0700163status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700164 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
165 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700166{
Mathias Agopian3330b202009-10-05 17:07:12 -0700167 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800168 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700169 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700170 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800171 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700172 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700173 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
174
Dan Stozad3182402014-11-17 12:03:59 -0800175 width = static_cast<int>(inWidth);
176 height = static_cast<int>(inHeight);
177 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700178 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700179 usage = inUsage;
180 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800181 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700182 }
183 return err;
184}
185
Chia-I Wub42f1712017-03-21 13:15:39 -0700186status_t GraphicBuffer::initWithHandle(const native_handle_t* handle,
187 HandleWrapMethod method, uint32_t width, uint32_t height,
Chris Forbes82c04982017-04-19 14:29:54 -0700188 PixelFormat format, uint32_t layerCount, uint64_t usage,
Chia-I Wub42f1712017-03-21 13:15:39 -0700189 uint32_t stride)
190{
Chia-I Wub42f1712017-03-21 13:15:39 -0700191 ANativeWindowBuffer::width = static_cast<int>(width);
192 ANativeWindowBuffer::height = static_cast<int>(height);
193 ANativeWindowBuffer::stride = static_cast<int>(stride);
194 ANativeWindowBuffer::format = format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700195 ANativeWindowBuffer::usage = usage;
196 ANativeWindowBuffer::usage_deprecated = int(usage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700197
198 ANativeWindowBuffer::layerCount = layerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700199
200 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
201
Chia-I Wucb8405e2017-04-17 15:20:19 -0700202 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
203 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700204 status_t err = mBufferMapper.importBuffer(handle, width, height,
205 layerCount, format, usage, stride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700206 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700207 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700208
209 return err;
210 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700211
212 if (method == TAKE_UNREGISTERED_HANDLE) {
213 native_handle_close(handle);
214 native_handle_delete(const_cast<native_handle_t*>(handle));
215 }
216
217 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700218 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700219 }
220
Chia-I Wucb8405e2017-04-17 15:20:19 -0700221 ANativeWindowBuffer::handle = handle;
222
Chia-I Wub42f1712017-03-21 13:15:39 -0700223 return NO_ERROR;
224}
225
Dan Stozad3182402014-11-17 12:03:59 -0800226status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700227{
228 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800229 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700230 return res;
231}
232
Dan Stozad3182402014-11-17 12:03:59 -0800233status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700234{
Dan Stozad3182402014-11-17 12:03:59 -0800235 if (rect.left < 0 || rect.right > width ||
236 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000237 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800238 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800239 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700240 return BAD_VALUE;
241 }
Dan Stozad3182402014-11-17 12:03:59 -0800242 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700243 return res;
244}
245
Dan Stozad3182402014-11-17 12:03:59 -0800246status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700247{
248 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800249 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700250 return res;
251}
252
Dan Stozad3182402014-11-17 12:03:59 -0800253status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
254 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700255{
Dan Stozad3182402014-11-17 12:03:59 -0800256 if (rect.left < 0 || rect.right > width ||
257 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700258 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
259 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800260 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700261 return BAD_VALUE;
262 }
Dan Stozad3182402014-11-17 12:03:59 -0800263 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700264 return res;
265}
266
Mathias Agopian3330b202009-10-05 17:07:12 -0700267status_t GraphicBuffer::unlock()
268{
269 status_t res = getBufferMapper().unlock(handle);
270 return res;
271}
272
Dan Stozad3182402014-11-17 12:03:59 -0800273status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300274{
275 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800276 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300277 return res;
278}
279
Dan Stozad3182402014-11-17 12:03:59 -0800280status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
281 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300282{
Craig Donnere96a3252017-02-02 12:13:34 -0800283 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
284}
285
286status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
287 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
288{
Dan Stozad3182402014-11-17 12:03:59 -0800289 if (rect.left < 0 || rect.right > width ||
290 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300291 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
292 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800293 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300294 return BAD_VALUE;
295 }
Craig Donnere96a3252017-02-02 12:13:34 -0800296 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
297 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300298 return res;
299}
300
Dan Stozad3182402014-11-17 12:03:59 -0800301status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
302 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300303{
304 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800305 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300306 return res;
307}
308
Dan Stozad3182402014-11-17 12:03:59 -0800309status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
310 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300311{
Dan Stozad3182402014-11-17 12:03:59 -0800312 if (rect.left < 0 || rect.right > width ||
313 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300314 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
315 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800316 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300317 return BAD_VALUE;
318 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700319 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300320 return res;
321}
322
323status_t GraphicBuffer::unlockAsync(int *fenceFd)
324{
325 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
326 return res;
327}
328
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800329size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700330 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800331}
Mathias Agopian3330b202009-10-05 17:07:12 -0700332
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800333size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700334 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800335}
336
Mathias Agopiane1424282013-07-29 21:24:40 -0700337status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800338 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
339 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700340
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800341 size_t fdCountNeeded = GraphicBuffer::getFdCount();
342 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700343
Dan Stozab1363d32014-03-28 15:10:52 -0700344 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700345 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800346 buf[1] = width;
347 buf[2] = height;
348 buf[3] = stride;
349 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700350 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700351 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700352 buf[7] = static_cast<int32_t>(mId >> 32);
353 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
354 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700355 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700356 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700357 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800358
359 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700360 buf[10] = int32_t(mTransportNumFds);
361 buf[11] = int32_t(mTransportNumInts);
362 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700363 memcpy(buf + 13, handle->data + handle->numFds,
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700364 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700365 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800366
Dan Stozaeea6d682015-04-20 12:07:13 -0700367 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700368 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700369 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700370 fds += mTransportNumFds;
371 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700372 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700373
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800374 return NO_ERROR;
375}
376
Mathias Agopiane1424282013-07-29 21:24:40 -0700377status_t GraphicBuffer::unflatten(
378 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800379
380 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700381
382 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
383 // see H2BGraphicBufferProducer.cpp
384 uint32_t flattenWordCount = 0;
385 if (buf[0] == 'GB01') {
386 // new version with 64-bits usage bits
387 flattenWordCount = 13;
388 } else if (buf[0] == 'GBFR') {
389 // old version, when usage bits were 32-bits
390 flattenWordCount = 12;
391 } else {
392 return BAD_TYPE;
393 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800394
Craig Donner6ebc46a2016-10-21 15:23:44 -0700395 const size_t numFds = static_cast<size_t>(buf[10]);
396 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800397
Michael Lentinec168b8a2015-02-18 10:14:18 -0800398 // Limit the maxNumber to be relatively small. The number of fds or ints
399 // should not come close to this number, and the number itself was simply
400 // chosen to be high enough to not cause issues and low enough to prevent
401 // overflow problems.
402 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700403 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
404 width = height = stride = format = usage_deprecated = 0;
405 layerCount = 0;
406 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700407 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700408 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700409 return BAD_VALUE;
410 }
411
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700412 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800413 if (size < sizeNeeded) return NO_MEMORY;
414
Michael Lentine38803262014-10-31 15:25:03 -0700415 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800416 if (count < fdCountNeeded) return NO_MEMORY;
417
418 if (handle) {
419 // free previous handle if any
420 free_handle();
421 }
422
423 if (numFds || numInts) {
424 width = buf[1];
425 height = buf[2];
426 stride = buf[3];
427 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700428 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700429 usage_deprecated = buf[6];
430 if (flattenWordCount == 13) {
431 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
432 } else {
433 usage = uint64_t(usage_deprecated);
434 }
Dan Stozad3182402014-11-17 12:03:59 -0800435 native_handle* h = native_handle_create(
436 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700437 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700438 width = height = stride = format = usage_deprecated = 0;
439 layerCount = 0;
440 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700441 handle = NULL;
442 ALOGE("unflatten: native_handle_create failed");
443 return NO_MEMORY;
444 }
Dan Stozad3182402014-11-17 12:03:59 -0800445 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700446 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800447 handle = h;
448 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700449 width = height = stride = format = usage_deprecated = 0;
450 layerCount = 0;
451 usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800452 handle = NULL;
453 }
454
Craig Donner6ebc46a2016-10-21 15:23:44 -0700455 mId = static_cast<uint64_t>(buf[7]) << 32;
456 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700457
Craig Donner6ebc46a2016-10-21 15:23:44 -0700458 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700459
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800460 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700461
462 if (handle != 0) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700463 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700464 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
465 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700466 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700467 width = height = stride = format = usage_deprecated = 0;
468 layerCount = 0;
469 usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800470 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700471 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700472 return err;
473 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700474
475 native_handle_close(handle);
476 native_handle_delete(const_cast<native_handle_t*>(handle));
477 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700478 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700479 }
480
Dan Stozaeea6d682015-04-20 12:07:13 -0700481 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700482 size -= sizeNeeded;
483 fds += numFds;
484 count -= numFds;
485
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800486 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700487}
488
Mathias Agopian3330b202009-10-05 17:07:12 -0700489// ---------------------------------------------------------------------------
490
491}; // namespace android