blob: 97b948d97932e38ce46294f12a7f7aa85d48650c [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,
Dan Stoza024e9312016-08-24 12:17:29 -070057 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
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 Stoza024e9312016-08-24 12:17:29 -070067 mInitCheck = initSize(inWidth, inHeight, inFormat, inUsage,
68 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,
72 PixelFormat inFormat, uint32_t inUsage, uint32_t inStride,
73 native_handle_t* inHandle, bool keepOwnership)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070074 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
75 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070076 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070077{
Dan Stozad3182402014-11-17 12:03:59 -080078 width = static_cast<int>(inWidth);
79 height = static_cast<int>(inHeight);
80 stride = static_cast<int>(inStride);
Mathias Agopian54ba51d2009-10-26 20:12:37 -070081 format = inFormat;
Dan Stozad3182402014-11-17 12:03:59 -080082 usage = static_cast<int>(inUsage);
Mathias Agopian54ba51d2009-10-26 20:12:37 -070083 handle = inHandle;
84}
85
Iliyan Malchev697526b2011-05-01 11:33:26 -070086GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070087 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
88 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070089 mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId()),
90 mGenerationNumber(0)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070091{
92 width = buffer->width;
93 height = buffer->height;
94 stride = buffer->stride;
95 format = buffer->format;
96 usage = buffer->usage;
97 handle = buffer->handle;
98}
99
Mathias Agopian3330b202009-10-05 17:07:12 -0700100GraphicBuffer::~GraphicBuffer()
101{
102 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800103 free_handle();
104 }
105}
106
107void GraphicBuffer::free_handle()
108{
109 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700110 mBufferMapper.unregisterBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800111 native_handle_close(handle);
112 native_handle_delete(const_cast<native_handle*>(handle));
113 } else if (mOwner == ownData) {
114 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
115 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700116 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700117 handle = NULL;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700118 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700119}
120
121status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800122 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700123}
124
Mathias Agopian678bdd62010-12-03 17:33:09 -0800125void GraphicBuffer::dumpAllocationsToSystemLog()
126{
127 GraphicBufferAllocator::dumpToSystemLog();
128}
129
Iliyan Malchev697526b2011-05-01 11:33:26 -0700130ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700131{
Colin Cross18fae752014-07-22 15:55:08 -0700132 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700133 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700134 const_cast<GraphicBuffer*>(this));
135}
136
Dan Stozad3182402014-11-17 12:03:59 -0800137status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
138 PixelFormat inFormat, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700139{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700140 if (mOwner != ownData)
141 return INVALID_OPERATION;
142
Dan Stozad3182402014-11-17 12:03:59 -0800143 if (handle &&
144 static_cast<int>(inWidth) == width &&
145 static_cast<int>(inHeight) == height &&
146 inFormat == format &&
147 static_cast<int>(inUsage) == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700148 return NO_ERROR;
149
Mathias Agopian3330b202009-10-05 17:07:12 -0700150 if (handle) {
151 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
152 allocator.free(handle);
153 handle = 0;
154 }
Dan Stoza024e9312016-08-24 12:17:29 -0700155 return initSize(inWidth, inHeight, inFormat, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700156}
157
Dan Stoza9de72932015-04-16 17:28:43 -0700158bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
159 PixelFormat inFormat, uint32_t inUsage)
160{
161 if (static_cast<int>(inWidth) != width) return true;
162 if (static_cast<int>(inHeight) != height) return true;
163 if (inFormat != format) return true;
164 if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
165 return false;
166}
167
Dan Stozad3182402014-11-17 12:03:59 -0800168status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -0700169 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700170{
Mathias Agopian3330b202009-10-05 17:07:12 -0700171 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800172 uint32_t outStride = 0;
Dan Stoza8deb4da2016-06-01 18:21:44 -0700173 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inUsage,
Dan Stoza024e9312016-08-24 12:17:29 -0700174 &handle, &outStride, mId, std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700175 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800176 width = static_cast<int>(inWidth);
177 height = static_cast<int>(inHeight);
178 format = inFormat;
179 usage = static_cast<int>(inUsage);
180 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700181 }
182 return err;
183}
184
Dan Stozad3182402014-11-17 12:03:59 -0800185status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700186{
187 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800188 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700189 return res;
190}
191
Dan Stozad3182402014-11-17 12:03:59 -0800192status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700193{
Dan Stozad3182402014-11-17 12:03:59 -0800194 if (rect.left < 0 || rect.right > width ||
195 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000196 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800197 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800198 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700199 return BAD_VALUE;
200 }
Dan Stozad3182402014-11-17 12:03:59 -0800201 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700202 return res;
203}
204
Dan Stozad3182402014-11-17 12:03:59 -0800205status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700206{
207 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800208 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700209 return res;
210}
211
Dan Stozad3182402014-11-17 12:03:59 -0800212status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
213 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700214{
Dan Stozad3182402014-11-17 12:03:59 -0800215 if (rect.left < 0 || rect.right > width ||
216 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700217 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
218 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800219 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700220 return BAD_VALUE;
221 }
Dan Stozad3182402014-11-17 12:03:59 -0800222 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700223 return res;
224}
225
Mathias Agopian3330b202009-10-05 17:07:12 -0700226status_t GraphicBuffer::unlock()
227{
228 status_t res = getBufferMapper().unlock(handle);
229 return res;
230}
231
Dan Stozad3182402014-11-17 12:03:59 -0800232status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300233{
234 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800235 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300236 return res;
237}
238
Dan Stozad3182402014-11-17 12:03:59 -0800239status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
240 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300241{
Dan Stozad3182402014-11-17 12:03:59 -0800242 if (rect.left < 0 || rect.right > width ||
243 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300244 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
245 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800246 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300247 return BAD_VALUE;
248 }
Dan Stozad3182402014-11-17 12:03:59 -0800249 status_t res = getBufferMapper().lockAsync(handle, inUsage, rect, vaddr,
250 fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300251 return res;
252}
253
Dan Stozad3182402014-11-17 12:03:59 -0800254status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
255 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300256{
257 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800258 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300259 return res;
260}
261
Dan Stozad3182402014-11-17 12:03:59 -0800262status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
263 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300264{
Dan Stozad3182402014-11-17 12:03:59 -0800265 if (rect.left < 0 || rect.right > width ||
266 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300267 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
268 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800269 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300270 return BAD_VALUE;
271 }
Dan Stozad3182402014-11-17 12:03:59 -0800272 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
273 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300274 return res;
275}
276
277status_t GraphicBuffer::unlockAsync(int *fenceFd)
278{
279 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
280 return res;
281}
282
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800283size_t GraphicBuffer::getFlattenedSize() const {
Dan Stoza812ed062015-06-02 15:45:22 -0700284 return static_cast<size_t>(11 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800285}
Mathias Agopian3330b202009-10-05 17:07:12 -0700286
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800287size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800288 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800289}
290
Mathias Agopiane1424282013-07-29 21:24:40 -0700291status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800292 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
293 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700294
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800295 size_t fdCountNeeded = GraphicBuffer::getFdCount();
296 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700297
Dan Stozab1363d32014-03-28 15:10:52 -0700298 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800299 buf[0] = 'GBFR';
300 buf[1] = width;
301 buf[2] = height;
302 buf[3] = stride;
303 buf[4] = format;
304 buf[5] = usage;
Dan Stozab1363d32014-03-28 15:10:52 -0700305 buf[6] = static_cast<int32_t>(mId >> 32);
306 buf[7] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
Dan Stoza812ed062015-06-02 15:45:22 -0700307 buf[8] = static_cast<int32_t>(mGenerationNumber);
Dan Stozab1363d32014-03-28 15:10:52 -0700308 buf[9] = 0;
Dan Stoza812ed062015-06-02 15:45:22 -0700309 buf[10] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800310
311 if (handle) {
Dan Stoza812ed062015-06-02 15:45:22 -0700312 buf[9] = handle->numFds;
313 buf[10] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800314 memcpy(fds, handle->data,
315 static_cast<size_t>(handle->numFds) * sizeof(int));
Dan Stoza812ed062015-06-02 15:45:22 -0700316 memcpy(&buf[11], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800317 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700318 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800319
Dan Stozaeea6d682015-04-20 12:07:13 -0700320 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700321 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700322 if (handle) {
323 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800324 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700325 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700326
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800327 return NO_ERROR;
328}
329
Mathias Agopiane1424282013-07-29 21:24:40 -0700330status_t GraphicBuffer::unflatten(
331 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Dan Stoza812ed062015-06-02 15:45:22 -0700332 if (size < 11 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800333
334 int const* buf = static_cast<int const*>(buffer);
335 if (buf[0] != 'GBFR') return BAD_TYPE;
336
Dan Stoza812ed062015-06-02 15:45:22 -0700337 const size_t numFds = static_cast<size_t>(buf[9]);
338 const size_t numInts = static_cast<size_t>(buf[10]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800339
Michael Lentinec168b8a2015-02-18 10:14:18 -0800340 // Limit the maxNumber to be relatively small. The number of fds or ints
341 // should not come close to this number, and the number itself was simply
342 // chosen to be high enough to not cause issues and low enough to prevent
343 // overflow problems.
344 const size_t maxNumber = 4096;
Dan Stoza812ed062015-06-02 15:45:22 -0700345 if (numFds >= maxNumber || numInts >= (maxNumber - 11)) {
Michael Lentine38803262014-10-31 15:25:03 -0700346 width = height = stride = format = usage = 0;
347 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800348 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700349 numFds, numInts);
350 return BAD_VALUE;
351 }
352
Dan Stoza812ed062015-06-02 15:45:22 -0700353 const size_t sizeNeeded = (11 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800354 if (size < sizeNeeded) return NO_MEMORY;
355
Michael Lentine38803262014-10-31 15:25:03 -0700356 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800357 if (count < fdCountNeeded) return NO_MEMORY;
358
359 if (handle) {
360 // free previous handle if any
361 free_handle();
362 }
363
364 if (numFds || numInts) {
365 width = buf[1];
366 height = buf[2];
367 stride = buf[3];
368 format = buf[4];
369 usage = buf[5];
Dan Stozad3182402014-11-17 12:03:59 -0800370 native_handle* h = native_handle_create(
371 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700372 if (!h) {
373 width = height = stride = format = usage = 0;
374 handle = NULL;
375 ALOGE("unflatten: native_handle_create failed");
376 return NO_MEMORY;
377 }
Dan Stozad3182402014-11-17 12:03:59 -0800378 memcpy(h->data, fds, numFds * sizeof(int));
Dan Stoza812ed062015-06-02 15:45:22 -0700379 memcpy(h->data + numFds, &buf[11], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800380 handle = h;
381 } else {
382 width = height = stride = format = usage = 0;
383 handle = NULL;
384 }
385
Dan Stozab1363d32014-03-28 15:10:52 -0700386 mId = static_cast<uint64_t>(buf[6]) << 32;
387 mId |= static_cast<uint32_t>(buf[7]);
388
Dan Stoza812ed062015-06-02 15:45:22 -0700389 mGenerationNumber = static_cast<uint32_t>(buf[8]);
390
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800391 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700392
393 if (handle != 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700394 status_t err = mBufferMapper.registerBuffer(this);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700395 if (err != NO_ERROR) {
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800396 width = height = stride = format = usage = 0;
397 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700398 ALOGE("unflatten: registerBuffer failed: %s (%d)",
399 strerror(-err), err);
400 return err;
401 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700402 }
403
Dan Stozaeea6d682015-04-20 12:07:13 -0700404 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700405 size -= sizeNeeded;
406 fds += numFds;
407 count -= numFds;
408
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800409 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700410}
411
Mathias Agopian3330b202009-10-05 17:07:12 -0700412// ---------------------------------------------------------------------------
413
414}; // namespace android