blob: 72d390c5ed8e69bc3a7a009e2b867a42efb6d130 [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 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700116 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700117}
118
119status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800120 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700121}
122
Mathias Agopian678bdd62010-12-03 17:33:09 -0800123void GraphicBuffer::dumpAllocationsToSystemLog()
124{
125 GraphicBufferAllocator::dumpToSystemLog();
126}
127
Iliyan Malchev697526b2011-05-01 11:33:26 -0700128ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700129{
Colin Cross18fae752014-07-22 15:55:08 -0700130 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700131 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700132 const_cast<GraphicBuffer*>(this));
133}
134
Dan Stozad3182402014-11-17 12:03:59 -0800135status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
136 PixelFormat inFormat, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700137{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700138 if (mOwner != ownData)
139 return INVALID_OPERATION;
140
Dan Stozad3182402014-11-17 12:03:59 -0800141 if (handle &&
142 static_cast<int>(inWidth) == width &&
143 static_cast<int>(inHeight) == height &&
144 inFormat == format &&
145 static_cast<int>(inUsage) == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700146 return NO_ERROR;
147
Mathias Agopian3330b202009-10-05 17:07:12 -0700148 if (handle) {
149 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
150 allocator.free(handle);
151 handle = 0;
152 }
Dan Stozad3182402014-11-17 12:03:59 -0800153 return initSize(inWidth, inHeight, inFormat, inUsage);
Mathias Agopian3330b202009-10-05 17:07:12 -0700154}
155
Dan Stoza9de72932015-04-16 17:28:43 -0700156bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
157 PixelFormat inFormat, uint32_t inUsage)
158{
159 if (static_cast<int>(inWidth) != width) return true;
160 if (static_cast<int>(inHeight) != height) return true;
161 if (inFormat != format) return true;
162 if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
163 return false;
164}
165
Dan Stozad3182402014-11-17 12:03:59 -0800166status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
167 PixelFormat inFormat, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700168{
Mathias Agopian3330b202009-10-05 17:07:12 -0700169 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800170 uint32_t outStride = 0;
171 status_t err = allocator.alloc(inWidth, inHeight, inFormat, inUsage,
172 &handle, &outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700173 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800174 width = static_cast<int>(inWidth);
175 height = static_cast<int>(inHeight);
176 format = inFormat;
177 usage = static_cast<int>(inUsage);
178 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700179 }
180 return err;
181}
182
Dan Stozad3182402014-11-17 12:03:59 -0800183status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700184{
185 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800186 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700187 return res;
188}
189
Dan Stozad3182402014-11-17 12:03:59 -0800190status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700191{
Dan Stozad3182402014-11-17 12:03:59 -0800192 if (rect.left < 0 || rect.right > width ||
193 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000194 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800195 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800196 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700197 return BAD_VALUE;
198 }
Dan Stozad3182402014-11-17 12:03:59 -0800199 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700200 return res;
201}
202
Dan Stozad3182402014-11-17 12:03:59 -0800203status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700204{
205 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800206 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700207 return res;
208}
209
Dan Stozad3182402014-11-17 12:03:59 -0800210status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
211 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700212{
Dan Stozad3182402014-11-17 12:03:59 -0800213 if (rect.left < 0 || rect.right > width ||
214 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700215 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
216 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800217 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700218 return BAD_VALUE;
219 }
Dan Stozad3182402014-11-17 12:03:59 -0800220 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700221 return res;
222}
223
Mathias Agopian3330b202009-10-05 17:07:12 -0700224status_t GraphicBuffer::unlock()
225{
226 status_t res = getBufferMapper().unlock(handle);
227 return res;
228}
229
Dan Stozad3182402014-11-17 12:03:59 -0800230status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300231{
232 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800233 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300234 return res;
235}
236
Dan Stozad3182402014-11-17 12:03:59 -0800237status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
238 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300239{
Dan Stozad3182402014-11-17 12:03:59 -0800240 if (rect.left < 0 || rect.right > width ||
241 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300242 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
243 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800244 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300245 return BAD_VALUE;
246 }
Dan Stozad3182402014-11-17 12:03:59 -0800247 status_t res = getBufferMapper().lockAsync(handle, inUsage, rect, vaddr,
248 fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300249 return res;
250}
251
Dan Stozad3182402014-11-17 12:03:59 -0800252status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
253 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300254{
255 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800256 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300257 return res;
258}
259
Dan Stozad3182402014-11-17 12:03:59 -0800260status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
261 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300262{
Dan Stozad3182402014-11-17 12:03:59 -0800263 if (rect.left < 0 || rect.right > width ||
264 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300265 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
266 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800267 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300268 return BAD_VALUE;
269 }
Dan Stozad3182402014-11-17 12:03:59 -0800270 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
271 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300272 return res;
273}
274
275status_t GraphicBuffer::unlockAsync(int *fenceFd)
276{
277 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
278 return res;
279}
280
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800281size_t GraphicBuffer::getFlattenedSize() const {
Dan Stoza812ed062015-06-02 15:45:22 -0700282 return static_cast<size_t>(11 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800283}
Mathias Agopian3330b202009-10-05 17:07:12 -0700284
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800285size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800286 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800287}
288
Mathias Agopiane1424282013-07-29 21:24:40 -0700289status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800290 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
291 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700292
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800293 size_t fdCountNeeded = GraphicBuffer::getFdCount();
294 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700295
Dan Stozab1363d32014-03-28 15:10:52 -0700296 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800297 buf[0] = 'GBFR';
298 buf[1] = width;
299 buf[2] = height;
300 buf[3] = stride;
301 buf[4] = format;
302 buf[5] = usage;
Dan Stozab1363d32014-03-28 15:10:52 -0700303 buf[6] = static_cast<int32_t>(mId >> 32);
304 buf[7] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
Dan Stoza812ed062015-06-02 15:45:22 -0700305 buf[8] = static_cast<int32_t>(mGenerationNumber);
Dan Stozab1363d32014-03-28 15:10:52 -0700306 buf[9] = 0;
Dan Stoza812ed062015-06-02 15:45:22 -0700307 buf[10] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800308
309 if (handle) {
Dan Stoza812ed062015-06-02 15:45:22 -0700310 buf[9] = handle->numFds;
311 buf[10] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800312 memcpy(fds, handle->data,
313 static_cast<size_t>(handle->numFds) * sizeof(int));
Dan Stoza812ed062015-06-02 15:45:22 -0700314 memcpy(&buf[11], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800315 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700316 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800317
Dan Stozaeea6d682015-04-20 12:07:13 -0700318 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700319 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700320 if (handle) {
321 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800322 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700323 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700324
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800325 return NO_ERROR;
326}
327
Mathias Agopiane1424282013-07-29 21:24:40 -0700328status_t GraphicBuffer::unflatten(
329 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Dan Stoza812ed062015-06-02 15:45:22 -0700330 if (size < 11 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800331
332 int const* buf = static_cast<int const*>(buffer);
333 if (buf[0] != 'GBFR') return BAD_TYPE;
334
Dan Stoza812ed062015-06-02 15:45:22 -0700335 const size_t numFds = static_cast<size_t>(buf[9]);
336 const size_t numInts = static_cast<size_t>(buf[10]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800337
Michael Lentinec168b8a2015-02-18 10:14:18 -0800338 // Limit the maxNumber to be relatively small. The number of fds or ints
339 // should not come close to this number, and the number itself was simply
340 // chosen to be high enough to not cause issues and low enough to prevent
341 // overflow problems.
342 const size_t maxNumber = 4096;
Dan Stoza812ed062015-06-02 15:45:22 -0700343 if (numFds >= maxNumber || numInts >= (maxNumber - 11)) {
Michael Lentine38803262014-10-31 15:25:03 -0700344 width = height = stride = format = usage = 0;
345 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800346 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700347 numFds, numInts);
348 return BAD_VALUE;
349 }
350
Dan Stoza812ed062015-06-02 15:45:22 -0700351 const size_t sizeNeeded = (11 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800352 if (size < sizeNeeded) return NO_MEMORY;
353
Michael Lentine38803262014-10-31 15:25:03 -0700354 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800355 if (count < fdCountNeeded) return NO_MEMORY;
356
357 if (handle) {
358 // free previous handle if any
359 free_handle();
360 }
361
362 if (numFds || numInts) {
363 width = buf[1];
364 height = buf[2];
365 stride = buf[3];
366 format = buf[4];
367 usage = buf[5];
Dan Stozad3182402014-11-17 12:03:59 -0800368 native_handle* h = native_handle_create(
369 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700370 if (!h) {
371 width = height = stride = format = usage = 0;
372 handle = NULL;
373 ALOGE("unflatten: native_handle_create failed");
374 return NO_MEMORY;
375 }
Dan Stozad3182402014-11-17 12:03:59 -0800376 memcpy(h->data, fds, numFds * sizeof(int));
Dan Stoza812ed062015-06-02 15:45:22 -0700377 memcpy(h->data + numFds, &buf[11], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800378 handle = h;
379 } else {
380 width = height = stride = format = usage = 0;
381 handle = NULL;
382 }
383
Dan Stozab1363d32014-03-28 15:10:52 -0700384 mId = static_cast<uint64_t>(buf[6]) << 32;
385 mId |= static_cast<uint32_t>(buf[7]);
386
Dan Stoza812ed062015-06-02 15:45:22 -0700387 mGenerationNumber = static_cast<uint32_t>(buf[8]);
388
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800389 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700390
391 if (handle != 0) {
Jamie Gennisd69097f2012-08-30 13:28:23 -0700392 status_t err = mBufferMapper.registerBuffer(handle);
393 if (err != NO_ERROR) {
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800394 width = height = stride = format = usage = 0;
395 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700396 ALOGE("unflatten: registerBuffer failed: %s (%d)",
397 strerror(-err), err);
398 return err;
399 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700400 }
401
Dan Stozaeea6d682015-04-20 12:07:13 -0700402 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700403 size -= sizeNeeded;
404 fds += numFds;
405 count -= numFds;
406
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800407 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700408}
409
Mathias Agopian3330b202009-10-05 17:07:12 -0700410// ---------------------------------------------------------------------------
411
412}; // namespace android