blob: 6e8473018ad9f501829c4a42cbd6b607f49aacfe [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
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080025#include <ui/GrallocMapper.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 Agopian3330b202009-10-05 17:07:12 -070042GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070043 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070044 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070045{
Dan Stoza01049c82014-11-11 10:32:31 -080046 width =
47 height =
48 stride =
49 format =
Mathias Agopian3330b202009-10-05 17:07:12 -070050 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080051 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070052 handle = NULL;
53}
54
Dan Stozad3182402014-11-17 12:03:59 -080055GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070056 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070057 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070058 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian3330b202009-10-05 17:07:12 -070059{
Dan Stoza01049c82014-11-11 10:32:31 -080060 width =
61 height =
62 stride =
63 format =
Mathias Agopian3330b202009-10-05 17:07:12 -070064 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080065 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070066 handle = NULL;
Craig Donnere96a3252017-02-02 12:13:34 -080067 mInitCheck = initSize(inWidth, inHeight, inFormat, 1, inUsage, inUsage,
Dan Stoza024e9312016-08-24 12:17:29 -070068 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -070069}
70
Dan Stozad3182402014-11-17 12:03:59 -080071GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Craig Donnere96a3252017-02-02 12:13:34 -080072 PixelFormat inFormat, uint32_t inLayerCount, uint64_t producerUsage,
73 uint64_t consumerUsage, std::string requestorName)
Craig Donner6ebc46a2016-10-21 15:23:44 -070074 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
75 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
76{
77 width =
78 height =
79 stride =
80 format =
Craig Donner6ebc46a2016-10-21 15:23:44 -070081 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080082 layerCount = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -070083 handle = NULL;
Craig Donnere96a3252017-02-02 12:13:34 -080084 mInitCheck = initSize(inWidth, inHeight, inFormat, inLayerCount,
85 producerUsage, consumerUsage, std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070086}
87
88GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
89 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
90 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070091 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
92 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070093 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070094{
Dan Stozad3182402014-11-17 12:03:59 -080095 width = static_cast<int>(inWidth);
96 height = static_cast<int>(inHeight);
97 stride = static_cast<int>(inStride);
Mathias Agopian54ba51d2009-10-26 20:12:37 -070098 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -070099 layerCount = inLayerCount;
Dan Stozad3182402014-11-17 12:03:59 -0800100 usage = static_cast<int>(inUsage);
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700101 handle = inHandle;
102}
103
Daniel Nicoara1c457102017-02-07 17:27:25 -0500104GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
105 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inProducerUsage,
106 uint32_t inConsumerUsage, uint32_t inStride,
107 native_handle_t* inHandle, bool keepOwnership)
108 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
109 mBufferMapper(GraphicBufferMapper::get()),
110 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
111{
112 width = static_cast<int>(inWidth);
113 height = static_cast<int>(inHeight);
114 stride = static_cast<int>(inStride);
115 format = inFormat;
116 layerCount = inLayerCount;
Jesse Hall79927812017-03-23 11:03:23 -0700117 usage = android_convertGralloc1To0Usage(inProducerUsage, inConsumerUsage);
Daniel Nicoara1c457102017-02-07 17:27:25 -0500118 handle = inHandle;
119}
120
121
Iliyan Malchev697526b2011-05-01 11:33:26 -0700122GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700123 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
124 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -0700125 mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId()),
126 mGenerationNumber(0)
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700127{
128 width = buffer->width;
129 height = buffer->height;
130 stride = buffer->stride;
131 format = buffer->format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700132 layerCount = buffer->layerCount;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700133 usage = buffer->usage;
134 handle = buffer->handle;
135}
136
Mathias Agopian3330b202009-10-05 17:07:12 -0700137GraphicBuffer::~GraphicBuffer()
138{
139 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800140 free_handle();
141 }
142}
143
144void GraphicBuffer::free_handle()
145{
146 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700147 mBufferMapper.unregisterBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800148 if (!mBufferMapper.getGrallocMapper().valid()) {
149 native_handle_close(handle);
150 native_handle_delete(const_cast<native_handle*>(handle));
151 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800152 } else if (mOwner == ownData) {
153 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
154 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700155 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700156 handle = NULL;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700157 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700158}
159
160status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800161 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700162}
163
Mathias Agopian678bdd62010-12-03 17:33:09 -0800164void GraphicBuffer::dumpAllocationsToSystemLog()
165{
166 GraphicBufferAllocator::dumpToSystemLog();
167}
168
Iliyan Malchev697526b2011-05-01 11:33:26 -0700169ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700170{
Colin Cross18fae752014-07-22 15:55:08 -0700171 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700172 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700173 const_cast<GraphicBuffer*>(this));
174}
175
Dan Stozad3182402014-11-17 12:03:59 -0800176status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700177 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700178{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700179 if (mOwner != ownData)
180 return INVALID_OPERATION;
181
Dan Stozad3182402014-11-17 12:03:59 -0800182 if (handle &&
183 static_cast<int>(inWidth) == width &&
184 static_cast<int>(inHeight) == height &&
185 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700186 inLayerCount == layerCount &&
Dan Stozad3182402014-11-17 12:03:59 -0800187 static_cast<int>(inUsage) == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700188 return NO_ERROR;
189
Mathias Agopian3330b202009-10-05 17:07:12 -0700190 if (handle) {
191 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
192 allocator.free(handle);
193 handle = 0;
194 }
Craig Donnere96a3252017-02-02 12:13:34 -0800195 return initSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, inUsage,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700196 "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700197}
198
Dan Stoza9de72932015-04-16 17:28:43 -0700199bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700200 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700201{
202 if (static_cast<int>(inWidth) != width) return true;
203 if (static_cast<int>(inHeight) != height) return true;
204 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700205 if (inLayerCount != layerCount) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700206 if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
207 return false;
208}
209
Dan Stozad3182402014-11-17 12:03:59 -0800210status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
Craig Donnere96a3252017-02-02 12:13:34 -0800211 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inProducerUsage,
212 uint64_t inConsumerUsage, std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700213{
Mathias Agopian3330b202009-10-05 17:07:12 -0700214 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800215 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700216 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Craig Donnere96a3252017-02-02 12:13:34 -0800217 inProducerUsage, inConsumerUsage, &handle, &outStride, mId,
218 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700219 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800220 width = static_cast<int>(inWidth);
221 height = static_cast<int>(inHeight);
222 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700223 layerCount = inLayerCount;
Jesse Hall79927812017-03-23 11:03:23 -0700224 usage = android_convertGralloc1To0Usage(inProducerUsage, inConsumerUsage);
Dan Stozad3182402014-11-17 12:03:59 -0800225 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700226 }
227 return err;
228}
229
Dan Stozad3182402014-11-17 12:03:59 -0800230status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700231{
232 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800233 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700234 return res;
235}
236
Dan Stozad3182402014-11-17 12:03:59 -0800237status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700238{
Dan Stozad3182402014-11-17 12:03:59 -0800239 if (rect.left < 0 || rect.right > width ||
240 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000241 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800242 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800243 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700244 return BAD_VALUE;
245 }
Dan Stozad3182402014-11-17 12:03:59 -0800246 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700247 return res;
248}
249
Dan Stozad3182402014-11-17 12:03:59 -0800250status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700251{
252 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800253 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700254 return res;
255}
256
Dan Stozad3182402014-11-17 12:03:59 -0800257status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
258 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700259{
Dan Stozad3182402014-11-17 12:03:59 -0800260 if (rect.left < 0 || rect.right > width ||
261 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700262 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
263 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800264 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700265 return BAD_VALUE;
266 }
Dan Stozad3182402014-11-17 12:03:59 -0800267 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700268 return res;
269}
270
Mathias Agopian3330b202009-10-05 17:07:12 -0700271status_t GraphicBuffer::unlock()
272{
273 status_t res = getBufferMapper().unlock(handle);
274 return res;
275}
276
Dan Stozad3182402014-11-17 12:03:59 -0800277status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300278{
279 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800280 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300281 return res;
282}
283
Dan Stozad3182402014-11-17 12:03:59 -0800284status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
285 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300286{
Craig Donnere96a3252017-02-02 12:13:34 -0800287 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
288}
289
290status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
291 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
292{
Dan Stozad3182402014-11-17 12:03:59 -0800293 if (rect.left < 0 || rect.right > width ||
294 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300295 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
296 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800297 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300298 return BAD_VALUE;
299 }
Craig Donnere96a3252017-02-02 12:13:34 -0800300 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
301 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300302 return res;
303}
304
Dan Stozad3182402014-11-17 12:03:59 -0800305status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
306 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300307{
308 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800309 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, 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, const Rect& rect,
314 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300315{
Dan Stozad3182402014-11-17 12:03:59 -0800316 if (rect.left < 0 || rect.right > width ||
317 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300318 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
319 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800320 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300321 return BAD_VALUE;
322 }
Dan Stozad3182402014-11-17 12:03:59 -0800323 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
324 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300325 return res;
326}
327
328status_t GraphicBuffer::unlockAsync(int *fenceFd)
329{
330 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
331 return res;
332}
333
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800334size_t GraphicBuffer::getFlattenedSize() const {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700335 return static_cast<size_t>(12 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800336}
Mathias Agopian3330b202009-10-05 17:07:12 -0700337
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800338size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800339 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800340}
341
Mathias Agopiane1424282013-07-29 21:24:40 -0700342status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800343 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
344 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700345
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800346 size_t fdCountNeeded = GraphicBuffer::getFdCount();
347 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700348
Dan Stozab1363d32014-03-28 15:10:52 -0700349 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800350 buf[0] = 'GBFR';
351 buf[1] = width;
352 buf[2] = height;
353 buf[3] = stride;
354 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700355 buf[5] = static_cast<int32_t>(layerCount);
356 buf[6] = usage;
357 buf[7] = static_cast<int32_t>(mId >> 32);
358 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
359 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700360 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700361 buf[11] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800362
363 if (handle) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700364 buf[10] = handle->numFds;
365 buf[11] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800366 memcpy(fds, handle->data,
367 static_cast<size_t>(handle->numFds) * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700368 memcpy(&buf[12], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800369 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700370 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800371
Dan Stozaeea6d682015-04-20 12:07:13 -0700372 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700373 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700374 if (handle) {
375 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800376 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700377 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700378
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800379 return NO_ERROR;
380}
381
Mathias Agopiane1424282013-07-29 21:24:40 -0700382status_t GraphicBuffer::unflatten(
383 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700384 if (size < 12 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800385
386 int const* buf = static_cast<int const*>(buffer);
387 if (buf[0] != 'GBFR') return BAD_TYPE;
388
Craig Donner6ebc46a2016-10-21 15:23:44 -0700389 const size_t numFds = static_cast<size_t>(buf[10]);
390 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800391
Michael Lentinec168b8a2015-02-18 10:14:18 -0800392 // Limit the maxNumber to be relatively small. The number of fds or ints
393 // should not come close to this number, and the number itself was simply
394 // chosen to be high enough to not cause issues and low enough to prevent
395 // overflow problems.
396 const size_t maxNumber = 4096;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700397 if (numFds >= maxNumber || numInts >= (maxNumber - 12)) {
398 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700399 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800400 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700401 numFds, numInts);
402 return BAD_VALUE;
403 }
404
Craig Donner6ebc46a2016-10-21 15:23:44 -0700405 const size_t sizeNeeded = (12 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800406 if (size < sizeNeeded) return NO_MEMORY;
407
Michael Lentine38803262014-10-31 15:25:03 -0700408 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800409 if (count < fdCountNeeded) return NO_MEMORY;
410
411 if (handle) {
412 // free previous handle if any
413 free_handle();
414 }
415
416 if (numFds || numInts) {
417 width = buf[1];
418 height = buf[2];
419 stride = buf[3];
420 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700421 layerCount = static_cast<uintptr_t>(buf[5]);
422 usage = buf[6];
Dan Stozad3182402014-11-17 12:03:59 -0800423 native_handle* h = native_handle_create(
424 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700425 if (!h) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700426 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700427 handle = NULL;
428 ALOGE("unflatten: native_handle_create failed");
429 return NO_MEMORY;
430 }
Dan Stozad3182402014-11-17 12:03:59 -0800431 memcpy(h->data, fds, numFds * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700432 memcpy(h->data + numFds, &buf[12], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800433 handle = h;
434 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700435 width = height = stride = format = layerCount = usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800436 handle = NULL;
437 }
438
Craig Donner6ebc46a2016-10-21 15:23:44 -0700439 mId = static_cast<uint64_t>(buf[7]) << 32;
440 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700441
Craig Donner6ebc46a2016-10-21 15:23:44 -0700442 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700443
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800444 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700445
446 if (handle != 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700447 status_t err = mBufferMapper.registerBuffer(this);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700448 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700449 width = height = stride = format = layerCount = usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800450 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700451 ALOGE("unflatten: registerBuffer failed: %s (%d)",
452 strerror(-err), err);
453 return err;
454 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700455 }
456
Dan Stozaeea6d682015-04-20 12:07:13 -0700457 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700458 size -= sizeNeeded;
459 fds += numFds;
460 count -= numFds;
461
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800462 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700463}
464
Mathias Agopian3330b202009-10-05 17:07:12 -0700465// ---------------------------------------------------------------------------
466
467}; // namespace android