blob: 6d729006f782ca0807145fb8349ae03547ebb9f7 [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
Chia-I Wu9ba189d2016-09-22 17:13:08 +080026#include <ui/GrallocMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070027#include <ui/GraphicBuffer.h>
28#include <ui/GraphicBufferAllocator.h>
29#include <ui/GraphicBufferMapper.h>
30#include <ui/PixelFormat.h>
31
Mathias Agopian3330b202009-10-05 17:07:12 -070032namespace android {
33
34// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070035// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070036// ===========================================================================
37
Dan Stozab1363d32014-03-28 15:10:52 -070038static uint64_t getUniqueId() {
39 static volatile int32_t nextId = 0;
40 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
41 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
42 return id;
43}
44
Mathias Agopian3330b202009-10-05 17:07:12 -070045GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070046 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070047 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070048{
Dan Stoza01049c82014-11-11 10:32:31 -080049 width =
50 height =
51 stride =
52 format =
Mathias Agopian3330b202009-10-05 17:07:12 -070053 usage = 0;
54 handle = NULL;
55}
56
Dan Stozad3182402014-11-17 12:03:59 -080057GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070058 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070059 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070060 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian3330b202009-10-05 17:07:12 -070061{
Dan Stoza01049c82014-11-11 10:32:31 -080062 width =
63 height =
64 stride =
65 format =
Mathias Agopian3330b202009-10-05 17:07:12 -070066 usage = 0;
67 handle = NULL;
Dan Stoza024e9312016-08-24 12:17:29 -070068 mInitCheck = initSize(inWidth, inHeight, inFormat, inUsage,
69 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -070070}
71
Dan Stozad3182402014-11-17 12:03:59 -080072GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
73 PixelFormat inFormat, uint32_t inUsage, uint32_t inStride,
74 native_handle_t* inHandle, bool keepOwnership)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070075 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
76 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070077 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070078{
Dan Stozad3182402014-11-17 12:03:59 -080079 width = static_cast<int>(inWidth);
80 height = static_cast<int>(inHeight);
81 stride = static_cast<int>(inStride);
Mathias Agopian54ba51d2009-10-26 20:12:37 -070082 format = inFormat;
Dan Stozad3182402014-11-17 12:03:59 -080083 usage = static_cast<int>(inUsage);
Mathias Agopian54ba51d2009-10-26 20:12:37 -070084 handle = inHandle;
85}
86
Iliyan Malchev697526b2011-05-01 11:33:26 -070087GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070088 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
89 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070090 mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId()),
91 mGenerationNumber(0)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070092{
93 width = buffer->width;
94 height = buffer->height;
95 stride = buffer->stride;
96 format = buffer->format;
97 usage = buffer->usage;
98 handle = buffer->handle;
99}
100
Mathias Agopian3330b202009-10-05 17:07:12 -0700101GraphicBuffer::~GraphicBuffer()
102{
103 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800104 free_handle();
105 }
106}
107
108void GraphicBuffer::free_handle()
109{
110 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700111 mBufferMapper.unregisterBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800112 if (!mBufferMapper.getGrallocMapper().valid()) {
113 native_handle_close(handle);
114 native_handle_delete(const_cast<native_handle*>(handle));
115 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800116 } else if (mOwner == ownData) {
117 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
118 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700119 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700120 handle = NULL;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700121 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700122}
123
124status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800125 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700126}
127
Mathias Agopian678bdd62010-12-03 17:33:09 -0800128void GraphicBuffer::dumpAllocationsToSystemLog()
129{
130 GraphicBufferAllocator::dumpToSystemLog();
131}
132
Iliyan Malchev697526b2011-05-01 11:33:26 -0700133ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700134{
Colin Cross18fae752014-07-22 15:55:08 -0700135 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700136 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700137 const_cast<GraphicBuffer*>(this));
138}
139
Dan Stozad3182402014-11-17 12:03:59 -0800140status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
141 PixelFormat inFormat, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700142{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700143 if (mOwner != ownData)
144 return INVALID_OPERATION;
145
Dan Stozad3182402014-11-17 12:03:59 -0800146 if (handle &&
147 static_cast<int>(inWidth) == width &&
148 static_cast<int>(inHeight) == height &&
149 inFormat == format &&
150 static_cast<int>(inUsage) == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700151 return NO_ERROR;
152
Mathias Agopian3330b202009-10-05 17:07:12 -0700153 if (handle) {
154 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
155 allocator.free(handle);
156 handle = 0;
157 }
Dan Stoza024e9312016-08-24 12:17:29 -0700158 return initSize(inWidth, inHeight, inFormat, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700159}
160
Dan Stoza9de72932015-04-16 17:28:43 -0700161bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
162 PixelFormat inFormat, uint32_t inUsage)
163{
164 if (static_cast<int>(inWidth) != width) return true;
165 if (static_cast<int>(inHeight) != height) return true;
166 if (inFormat != format) return true;
167 if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
168 return false;
169}
170
Dan Stozad3182402014-11-17 12:03:59 -0800171status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -0700172 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700173{
Mathias Agopian3330b202009-10-05 17:07:12 -0700174 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800175 uint32_t outStride = 0;
Dan Stoza8deb4da2016-06-01 18:21:44 -0700176 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inUsage,
Dan Stoza024e9312016-08-24 12:17:29 -0700177 &handle, &outStride, mId, std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700178 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800179 width = static_cast<int>(inWidth);
180 height = static_cast<int>(inHeight);
181 format = inFormat;
182 usage = static_cast<int>(inUsage);
183 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700184 }
185 return err;
186}
187
Dan Stozad3182402014-11-17 12:03:59 -0800188status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700189{
190 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800191 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700192 return res;
193}
194
Dan Stozad3182402014-11-17 12:03:59 -0800195status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700196{
Dan Stozad3182402014-11-17 12:03:59 -0800197 if (rect.left < 0 || rect.right > width ||
198 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000199 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800200 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800201 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700202 return BAD_VALUE;
203 }
Dan Stozad3182402014-11-17 12:03:59 -0800204 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700205 return res;
206}
207
Dan Stozad3182402014-11-17 12:03:59 -0800208status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700209{
210 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800211 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700212 return res;
213}
214
Dan Stozad3182402014-11-17 12:03:59 -0800215status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
216 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700217{
Dan Stozad3182402014-11-17 12:03:59 -0800218 if (rect.left < 0 || rect.right > width ||
219 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700220 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
221 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800222 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700223 return BAD_VALUE;
224 }
Dan Stozad3182402014-11-17 12:03:59 -0800225 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700226 return res;
227}
228
Mathias Agopian3330b202009-10-05 17:07:12 -0700229status_t GraphicBuffer::unlock()
230{
231 status_t res = getBufferMapper().unlock(handle);
232 return res;
233}
234
Dan Stozad3182402014-11-17 12:03:59 -0800235status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300236{
237 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800238 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300239 return res;
240}
241
Dan Stozad3182402014-11-17 12:03:59 -0800242status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
243 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300244{
Dan Stozad3182402014-11-17 12:03:59 -0800245 if (rect.left < 0 || rect.right > width ||
246 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300247 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
248 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800249 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300250 return BAD_VALUE;
251 }
Dan Stozad3182402014-11-17 12:03:59 -0800252 status_t res = getBufferMapper().lockAsync(handle, inUsage, rect, vaddr,
253 fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300254 return res;
255}
256
Dan Stozad3182402014-11-17 12:03:59 -0800257status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
258 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300259{
260 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800261 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300262 return res;
263}
264
Dan Stozad3182402014-11-17 12:03:59 -0800265status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
266 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300267{
Dan Stozad3182402014-11-17 12:03:59 -0800268 if (rect.left < 0 || rect.right > width ||
269 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300270 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
271 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800272 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300273 return BAD_VALUE;
274 }
Dan Stozad3182402014-11-17 12:03:59 -0800275 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
276 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300277 return res;
278}
279
280status_t GraphicBuffer::unlockAsync(int *fenceFd)
281{
282 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
283 return res;
284}
285
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800286size_t GraphicBuffer::getFlattenedSize() const {
Dan Stoza812ed062015-06-02 15:45:22 -0700287 return static_cast<size_t>(11 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800288}
Mathias Agopian3330b202009-10-05 17:07:12 -0700289
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800290size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800291 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800292}
293
Mathias Agopiane1424282013-07-29 21:24:40 -0700294status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800295 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
296 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700297
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800298 size_t fdCountNeeded = GraphicBuffer::getFdCount();
299 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700300
Dan Stozab1363d32014-03-28 15:10:52 -0700301 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800302 buf[0] = 'GBFR';
303 buf[1] = width;
304 buf[2] = height;
305 buf[3] = stride;
306 buf[4] = format;
307 buf[5] = usage;
Dan Stozab1363d32014-03-28 15:10:52 -0700308 buf[6] = static_cast<int32_t>(mId >> 32);
309 buf[7] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
Dan Stoza812ed062015-06-02 15:45:22 -0700310 buf[8] = static_cast<int32_t>(mGenerationNumber);
Dan Stozab1363d32014-03-28 15:10:52 -0700311 buf[9] = 0;
Dan Stoza812ed062015-06-02 15:45:22 -0700312 buf[10] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800313
314 if (handle) {
Dan Stoza812ed062015-06-02 15:45:22 -0700315 buf[9] = handle->numFds;
316 buf[10] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800317 memcpy(fds, handle->data,
318 static_cast<size_t>(handle->numFds) * sizeof(int));
Dan Stoza812ed062015-06-02 15:45:22 -0700319 memcpy(&buf[11], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800320 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700321 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800322
Dan Stozaeea6d682015-04-20 12:07:13 -0700323 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700324 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700325 if (handle) {
326 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800327 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700328 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700329
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800330 return NO_ERROR;
331}
332
Mathias Agopiane1424282013-07-29 21:24:40 -0700333status_t GraphicBuffer::unflatten(
334 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Dan Stoza812ed062015-06-02 15:45:22 -0700335 if (size < 11 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800336
337 int const* buf = static_cast<int const*>(buffer);
338 if (buf[0] != 'GBFR') return BAD_TYPE;
339
Dan Stoza812ed062015-06-02 15:45:22 -0700340 const size_t numFds = static_cast<size_t>(buf[9]);
341 const size_t numInts = static_cast<size_t>(buf[10]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800342
Michael Lentinec168b8a2015-02-18 10:14:18 -0800343 // Limit the maxNumber to be relatively small. The number of fds or ints
344 // should not come close to this number, and the number itself was simply
345 // chosen to be high enough to not cause issues and low enough to prevent
346 // overflow problems.
347 const size_t maxNumber = 4096;
Dan Stoza812ed062015-06-02 15:45:22 -0700348 if (numFds >= maxNumber || numInts >= (maxNumber - 11)) {
Michael Lentine38803262014-10-31 15:25:03 -0700349 width = height = stride = format = usage = 0;
350 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800351 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700352 numFds, numInts);
353 return BAD_VALUE;
354 }
355
Dan Stoza812ed062015-06-02 15:45:22 -0700356 const size_t sizeNeeded = (11 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800357 if (size < sizeNeeded) return NO_MEMORY;
358
Michael Lentine38803262014-10-31 15:25:03 -0700359 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800360 if (count < fdCountNeeded) return NO_MEMORY;
361
362 if (handle) {
363 // free previous handle if any
364 free_handle();
365 }
366
367 if (numFds || numInts) {
368 width = buf[1];
369 height = buf[2];
370 stride = buf[3];
371 format = buf[4];
372 usage = buf[5];
Dan Stozad3182402014-11-17 12:03:59 -0800373 native_handle* h = native_handle_create(
374 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700375 if (!h) {
376 width = height = stride = format = usage = 0;
377 handle = NULL;
378 ALOGE("unflatten: native_handle_create failed");
379 return NO_MEMORY;
380 }
Dan Stozad3182402014-11-17 12:03:59 -0800381 memcpy(h->data, fds, numFds * sizeof(int));
Dan Stoza812ed062015-06-02 15:45:22 -0700382 memcpy(h->data + numFds, &buf[11], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800383 handle = h;
384 } else {
385 width = height = stride = format = usage = 0;
386 handle = NULL;
387 }
388
Dan Stozab1363d32014-03-28 15:10:52 -0700389 mId = static_cast<uint64_t>(buf[6]) << 32;
390 mId |= static_cast<uint32_t>(buf[7]);
391
Dan Stoza812ed062015-06-02 15:45:22 -0700392 mGenerationNumber = static_cast<uint32_t>(buf[8]);
393
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800394 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700395
396 if (handle != 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700397 status_t err = mBufferMapper.registerBuffer(this);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700398 if (err != NO_ERROR) {
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800399 width = height = stride = format = usage = 0;
400 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700401 ALOGE("unflatten: registerBuffer failed: %s (%d)",
402 strerror(-err), err);
403 return err;
404 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700405 }
406
Dan Stozaeea6d682015-04-20 12:07:13 -0700407 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700408 size -= sizeNeeded;
409 fds += numFds;
410 count -= numFds;
411
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800412 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700413}
414
Mathias Agopian3330b202009-10-05 17:07:12 -0700415// ---------------------------------------------------------------------------
416
417}; // namespace android