blob: f28af6e200190feeb1ec216f06b8172a4e08a442 [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 <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Mathias Agopian3330b202009-10-05 17:07:12 -070023#include <utils/Errors.h>
24#include <utils/Log.h>
25
26#include <ui/GraphicBuffer.h>
27#include <ui/GraphicBufferAllocator.h>
28#include <ui/GraphicBufferMapper.h>
29#include <ui/PixelFormat.h>
30
Mathias Agopian3330b202009-10-05 17:07:12 -070031namespace android {
32
33// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070034// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070035// ===========================================================================
36
Dan Stozab1363d32014-03-28 15:10:52 -070037static uint64_t getUniqueId() {
38 static volatile int32_t nextId = 0;
39 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
40 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
41 return id;
42}
43
Mathias Agopian3330b202009-10-05 17:07:12 -070044GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070045 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070046 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070047{
Dan Stoza01049c82014-11-11 10:32:31 -080048 width =
49 height =
50 stride =
51 format =
Mathias Agopian3330b202009-10-05 17:07:12 -070052 usage = 0;
53 handle = NULL;
54}
55
Dan Stozad3182402014-11-17 12:03:59 -080056GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
57 PixelFormat inFormat, uint32_t inUsage)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070058 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070059 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian3330b202009-10-05 17:07:12 -070060{
Dan Stoza01049c82014-11-11 10:32:31 -080061 width =
62 height =
63 stride =
64 format =
Mathias Agopian3330b202009-10-05 17:07:12 -070065 usage = 0;
66 handle = NULL;
Dan Stozad3182402014-11-17 12:03:59 -080067 mInitCheck = initSize(inWidth, inHeight, inFormat, inUsage);
Mathias Agopian3330b202009-10-05 17:07:12 -070068}
69
Dan Stozad3182402014-11-17 12:03:59 -080070GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
71 PixelFormat inFormat, uint32_t inUsage, uint32_t inStride,
72 native_handle_t* inHandle, bool keepOwnership)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070073 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
74 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070075 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070076{
Dan Stozad3182402014-11-17 12:03:59 -080077 width = static_cast<int>(inWidth);
78 height = static_cast<int>(inHeight);
79 stride = static_cast<int>(inStride);
Mathias Agopian54ba51d2009-10-26 20:12:37 -070080 format = inFormat;
Dan Stozad3182402014-11-17 12:03:59 -080081 usage = static_cast<int>(inUsage);
Mathias Agopian54ba51d2009-10-26 20:12:37 -070082 handle = inHandle;
83}
84
Iliyan Malchev697526b2011-05-01 11:33:26 -070085GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070086 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
87 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070088 mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId()),
89 mGenerationNumber(0)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070090{
91 width = buffer->width;
92 height = buffer->height;
93 stride = buffer->stride;
94 format = buffer->format;
95 usage = buffer->usage;
96 handle = buffer->handle;
97}
98
Mathias Agopian3330b202009-10-05 17:07:12 -070099GraphicBuffer::~GraphicBuffer()
100{
101 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800102 free_handle();
103 }
104}
105
106void GraphicBuffer::free_handle()
107{
108 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700109 mBufferMapper.unregisterBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800110 native_handle_close(handle);
111 native_handle_delete(const_cast<native_handle*>(handle));
112 } else if (mOwner == ownData) {
113 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
114 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700115 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700116 handle = NULL;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700117 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700118}
119
120status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800121 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700122}
123
Mathias Agopian678bdd62010-12-03 17:33:09 -0800124void GraphicBuffer::dumpAllocationsToSystemLog()
125{
126 GraphicBufferAllocator::dumpToSystemLog();
127}
128
Iliyan Malchev697526b2011-05-01 11:33:26 -0700129ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700130{
Colin Cross18fae752014-07-22 15:55:08 -0700131 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700132 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700133 const_cast<GraphicBuffer*>(this));
134}
135
Dan Stozad3182402014-11-17 12:03:59 -0800136status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
137 PixelFormat inFormat, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700138{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700139 if (mOwner != ownData)
140 return INVALID_OPERATION;
141
Dan Stozad3182402014-11-17 12:03:59 -0800142 if (handle &&
143 static_cast<int>(inWidth) == width &&
144 static_cast<int>(inHeight) == height &&
145 inFormat == format &&
146 static_cast<int>(inUsage) == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700147 return NO_ERROR;
148
Mathias Agopian3330b202009-10-05 17:07:12 -0700149 if (handle) {
150 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
151 allocator.free(handle);
152 handle = 0;
153 }
Dan Stozad3182402014-11-17 12:03:59 -0800154 return initSize(inWidth, inHeight, inFormat, inUsage);
Mathias Agopian3330b202009-10-05 17:07:12 -0700155}
156
Dan Stoza9de72932015-04-16 17:28:43 -0700157bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
158 PixelFormat inFormat, uint32_t inUsage)
159{
160 if (static_cast<int>(inWidth) != width) return true;
161 if (static_cast<int>(inHeight) != height) return true;
162 if (inFormat != format) return true;
163 if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
164 return false;
165}
166
Dan Stozad3182402014-11-17 12:03:59 -0800167status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
168 PixelFormat inFormat, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700169{
Mathias Agopian3330b202009-10-05 17:07:12 -0700170 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800171 uint32_t outStride = 0;
Dan Stoza8deb4da2016-06-01 18:21:44 -0700172 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inUsage,
173 &handle, &outStride, mId);
Mathias Agopian3330b202009-10-05 17:07:12 -0700174 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800175 width = static_cast<int>(inWidth);
176 height = static_cast<int>(inHeight);
177 format = inFormat;
178 usage = static_cast<int>(inUsage);
179 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700180 }
181 return err;
182}
183
Dan Stozad3182402014-11-17 12:03:59 -0800184status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700185{
186 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800187 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700188 return res;
189}
190
Dan Stozad3182402014-11-17 12:03:59 -0800191status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700192{
Dan Stozad3182402014-11-17 12:03:59 -0800193 if (rect.left < 0 || rect.right > width ||
194 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000195 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800196 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800197 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700198 return BAD_VALUE;
199 }
Dan Stozad3182402014-11-17 12:03:59 -0800200 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700201 return res;
202}
203
Dan Stozad3182402014-11-17 12:03:59 -0800204status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700205{
206 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800207 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700208 return res;
209}
210
Dan Stozad3182402014-11-17 12:03:59 -0800211status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
212 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700213{
Dan Stozad3182402014-11-17 12:03:59 -0800214 if (rect.left < 0 || rect.right > width ||
215 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700216 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
217 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800218 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700219 return BAD_VALUE;
220 }
Dan Stozad3182402014-11-17 12:03:59 -0800221 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700222 return res;
223}
224
Mathias Agopian3330b202009-10-05 17:07:12 -0700225status_t GraphicBuffer::unlock()
226{
227 status_t res = getBufferMapper().unlock(handle);
228 return res;
229}
230
Dan Stozad3182402014-11-17 12:03:59 -0800231status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300232{
233 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800234 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300235 return res;
236}
237
Dan Stozad3182402014-11-17 12:03:59 -0800238status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
239 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300240{
Dan Stozad3182402014-11-17 12:03:59 -0800241 if (rect.left < 0 || rect.right > width ||
242 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300243 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
244 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800245 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300246 return BAD_VALUE;
247 }
Dan Stozad3182402014-11-17 12:03:59 -0800248 status_t res = getBufferMapper().lockAsync(handle, inUsage, rect, vaddr,
249 fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300250 return res;
251}
252
Dan Stozad3182402014-11-17 12:03:59 -0800253status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
254 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300255{
256 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800257 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300258 return res;
259}
260
Dan Stozad3182402014-11-17 12:03:59 -0800261status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
262 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300263{
Dan Stozad3182402014-11-17 12:03:59 -0800264 if (rect.left < 0 || rect.right > width ||
265 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300266 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
267 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800268 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300269 return BAD_VALUE;
270 }
Dan Stozad3182402014-11-17 12:03:59 -0800271 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
272 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300273 return res;
274}
275
276status_t GraphicBuffer::unlockAsync(int *fenceFd)
277{
278 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
279 return res;
280}
281
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800282size_t GraphicBuffer::getFlattenedSize() const {
Dan Stoza812ed062015-06-02 15:45:22 -0700283 return static_cast<size_t>(11 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800284}
Mathias Agopian3330b202009-10-05 17:07:12 -0700285
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800286size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800287 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800288}
289
Mathias Agopiane1424282013-07-29 21:24:40 -0700290status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800291 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
292 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700293
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800294 size_t fdCountNeeded = GraphicBuffer::getFdCount();
295 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700296
Dan Stozab1363d32014-03-28 15:10:52 -0700297 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800298 buf[0] = 'GBFR';
299 buf[1] = width;
300 buf[2] = height;
301 buf[3] = stride;
302 buf[4] = format;
303 buf[5] = usage;
Dan Stozab1363d32014-03-28 15:10:52 -0700304 buf[6] = static_cast<int32_t>(mId >> 32);
305 buf[7] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
Dan Stoza812ed062015-06-02 15:45:22 -0700306 buf[8] = static_cast<int32_t>(mGenerationNumber);
Dan Stozab1363d32014-03-28 15:10:52 -0700307 buf[9] = 0;
Dan Stoza812ed062015-06-02 15:45:22 -0700308 buf[10] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800309
310 if (handle) {
Dan Stoza812ed062015-06-02 15:45:22 -0700311 buf[9] = handle->numFds;
312 buf[10] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800313 memcpy(fds, handle->data,
314 static_cast<size_t>(handle->numFds) * sizeof(int));
Dan Stoza812ed062015-06-02 15:45:22 -0700315 memcpy(&buf[11], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800316 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700317 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800318
Dan Stozaeea6d682015-04-20 12:07:13 -0700319 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700320 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700321 if (handle) {
322 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800323 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700324 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700325
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800326 return NO_ERROR;
327}
328
Mathias Agopiane1424282013-07-29 21:24:40 -0700329status_t GraphicBuffer::unflatten(
330 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Dan Stoza812ed062015-06-02 15:45:22 -0700331 if (size < 11 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800332
333 int const* buf = static_cast<int const*>(buffer);
334 if (buf[0] != 'GBFR') return BAD_TYPE;
335
Dan Stoza812ed062015-06-02 15:45:22 -0700336 const size_t numFds = static_cast<size_t>(buf[9]);
337 const size_t numInts = static_cast<size_t>(buf[10]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800338
Michael Lentinec168b8a2015-02-18 10:14:18 -0800339 // Limit the maxNumber to be relatively small. The number of fds or ints
340 // should not come close to this number, and the number itself was simply
341 // chosen to be high enough to not cause issues and low enough to prevent
342 // overflow problems.
343 const size_t maxNumber = 4096;
Dan Stoza812ed062015-06-02 15:45:22 -0700344 if (numFds >= maxNumber || numInts >= (maxNumber - 11)) {
Michael Lentine38803262014-10-31 15:25:03 -0700345 width = height = stride = format = usage = 0;
346 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800347 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700348 numFds, numInts);
349 return BAD_VALUE;
350 }
351
Dan Stoza812ed062015-06-02 15:45:22 -0700352 const size_t sizeNeeded = (11 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800353 if (size < sizeNeeded) return NO_MEMORY;
354
Michael Lentine38803262014-10-31 15:25:03 -0700355 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800356 if (count < fdCountNeeded) return NO_MEMORY;
357
358 if (handle) {
359 // free previous handle if any
360 free_handle();
361 }
362
363 if (numFds || numInts) {
364 width = buf[1];
365 height = buf[2];
366 stride = buf[3];
367 format = buf[4];
368 usage = buf[5];
Dan Stozad3182402014-11-17 12:03:59 -0800369 native_handle* h = native_handle_create(
370 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700371 if (!h) {
372 width = height = stride = format = usage = 0;
373 handle = NULL;
374 ALOGE("unflatten: native_handle_create failed");
375 return NO_MEMORY;
376 }
Dan Stozad3182402014-11-17 12:03:59 -0800377 memcpy(h->data, fds, numFds * sizeof(int));
Dan Stoza812ed062015-06-02 15:45:22 -0700378 memcpy(h->data + numFds, &buf[11], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800379 handle = h;
380 } else {
381 width = height = stride = format = usage = 0;
382 handle = NULL;
383 }
384
Dan Stozab1363d32014-03-28 15:10:52 -0700385 mId = static_cast<uint64_t>(buf[6]) << 32;
386 mId |= static_cast<uint32_t>(buf[7]);
387
Dan Stoza812ed062015-06-02 15:45:22 -0700388 mGenerationNumber = static_cast<uint32_t>(buf[8]);
389
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800390 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700391
392 if (handle != 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700393 status_t err = mBufferMapper.registerBuffer(this);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700394 if (err != NO_ERROR) {
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800395 width = height = stride = format = usage = 0;
396 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700397 ALOGE("unflatten: registerBuffer failed: %s (%d)",
398 strerror(-err), err);
399 return err;
400 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700401 }
402
Dan Stozaeea6d682015-04-20 12:07:13 -0700403 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700404 size -= sizeNeeded;
405 fds += numFds;
406 count -= numFds;
407
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800408 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700409}
410
Mathias Agopian3330b202009-10-05 17:07:12 -0700411// ---------------------------------------------------------------------------
412
413}; // namespace android