blob: 07164a4f44f2ca703752d1f9e97d203d70e74ae3 [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 =
Craig Donner6ebc46a2016-10-21 15:23:44 -070053 layerCount =
Mathias Agopian3330b202009-10-05 17:07:12 -070054 usage = 0;
55 handle = NULL;
56}
57
Dan Stozad3182402014-11-17 12:03:59 -080058GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070059 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070060 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070061 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian3330b202009-10-05 17:07:12 -070062{
Dan Stoza01049c82014-11-11 10:32:31 -080063 width =
64 height =
65 stride =
66 format =
Craig Donner6ebc46a2016-10-21 15:23:44 -070067 layerCount =
Mathias Agopian3330b202009-10-05 17:07:12 -070068 usage = 0;
69 handle = NULL;
Craig Donner6ebc46a2016-10-21 15:23:44 -070070 mInitCheck = initSize(inWidth, inHeight, inFormat, 1, inUsage,
Dan Stoza024e9312016-08-24 12:17:29 -070071 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -070072}
73
Dan Stozad3182402014-11-17 12:03:59 -080074GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -070075 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
76 std::string requestorName)
77 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
78 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
79{
80 width =
81 height =
82 stride =
83 format =
84 layerCount =
85 usage = 0;
86 handle = NULL;
87 mInitCheck = initSize(inWidth, inHeight, inFormat, inLayerCount, inUsage,
88 std::move(requestorName));
89}
90
91GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
92 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
93 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070094 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
95 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070096 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070097{
Dan Stozad3182402014-11-17 12:03:59 -080098 width = static_cast<int>(inWidth);
99 height = static_cast<int>(inHeight);
100 stride = static_cast<int>(inStride);
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700101 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700102 layerCount = inLayerCount;
Dan Stozad3182402014-11-17 12:03:59 -0800103 usage = static_cast<int>(inUsage);
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700104 handle = inHandle;
105}
106
Iliyan Malchev697526b2011-05-01 11:33:26 -0700107GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700108 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
109 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -0700110 mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId()),
111 mGenerationNumber(0)
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700112{
113 width = buffer->width;
114 height = buffer->height;
115 stride = buffer->stride;
116 format = buffer->format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700117 layerCount = buffer->layerCount;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700118 usage = buffer->usage;
119 handle = buffer->handle;
120}
121
Mathias Agopian3330b202009-10-05 17:07:12 -0700122GraphicBuffer::~GraphicBuffer()
123{
124 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800125 free_handle();
126 }
127}
128
129void GraphicBuffer::free_handle()
130{
131 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700132 mBufferMapper.unregisterBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800133 if (!mBufferMapper.getGrallocMapper().valid()) {
134 native_handle_close(handle);
135 native_handle_delete(const_cast<native_handle*>(handle));
136 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800137 } else if (mOwner == ownData) {
138 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
139 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700140 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700141 handle = NULL;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700142 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700143}
144
145status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800146 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700147}
148
Mathias Agopian678bdd62010-12-03 17:33:09 -0800149void GraphicBuffer::dumpAllocationsToSystemLog()
150{
151 GraphicBufferAllocator::dumpToSystemLog();
152}
153
Iliyan Malchev697526b2011-05-01 11:33:26 -0700154ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700155{
Colin Cross18fae752014-07-22 15:55:08 -0700156 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700157 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700158 const_cast<GraphicBuffer*>(this));
159}
160
Dan Stozad3182402014-11-17 12:03:59 -0800161status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700162 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700163{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700164 if (mOwner != ownData)
165 return INVALID_OPERATION;
166
Dan Stozad3182402014-11-17 12:03:59 -0800167 if (handle &&
168 static_cast<int>(inWidth) == width &&
169 static_cast<int>(inHeight) == height &&
170 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700171 inLayerCount == layerCount &&
Dan Stozad3182402014-11-17 12:03:59 -0800172 static_cast<int>(inUsage) == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700173 return NO_ERROR;
174
Mathias Agopian3330b202009-10-05 17:07:12 -0700175 if (handle) {
176 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
177 allocator.free(handle);
178 handle = 0;
179 }
Craig Donner6ebc46a2016-10-21 15:23:44 -0700180 return initSize(inWidth, inHeight, inFormat, inLayerCount, inUsage,
181 "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700182}
183
Dan Stoza9de72932015-04-16 17:28:43 -0700184bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700185 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700186{
187 if (static_cast<int>(inWidth) != width) return true;
188 if (static_cast<int>(inHeight) != height) return true;
189 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700190 if (inLayerCount != layerCount) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700191 if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
192 return false;
193}
194
Dan Stozad3182402014-11-17 12:03:59 -0800195status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700196 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
197 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700198{
Mathias Agopian3330b202009-10-05 17:07:12 -0700199 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800200 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700201 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
202 inUsage, &handle, &outStride, mId, std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700203 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800204 width = static_cast<int>(inWidth);
205 height = static_cast<int>(inHeight);
206 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700207 layerCount = inLayerCount;
Dan Stozad3182402014-11-17 12:03:59 -0800208 usage = static_cast<int>(inUsage);
209 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700210 }
211 return err;
212}
213
Dan Stozad3182402014-11-17 12:03:59 -0800214status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700215{
216 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800217 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700218 return res;
219}
220
Dan Stozad3182402014-11-17 12:03:59 -0800221status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700222{
Dan Stozad3182402014-11-17 12:03:59 -0800223 if (rect.left < 0 || rect.right > width ||
224 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000225 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800226 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800227 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700228 return BAD_VALUE;
229 }
Dan Stozad3182402014-11-17 12:03:59 -0800230 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700231 return res;
232}
233
Dan Stozad3182402014-11-17 12:03:59 -0800234status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700235{
236 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800237 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700238 return res;
239}
240
Dan Stozad3182402014-11-17 12:03:59 -0800241status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
242 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700243{
Dan Stozad3182402014-11-17 12:03:59 -0800244 if (rect.left < 0 || rect.right > width ||
245 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700246 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
247 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800248 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700249 return BAD_VALUE;
250 }
Dan Stozad3182402014-11-17 12:03:59 -0800251 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700252 return res;
253}
254
Mathias Agopian3330b202009-10-05 17:07:12 -0700255status_t GraphicBuffer::unlock()
256{
257 status_t res = getBufferMapper().unlock(handle);
258 return res;
259}
260
Dan Stozad3182402014-11-17 12:03:59 -0800261status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300262{
263 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800264 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300265 return res;
266}
267
Dan Stozad3182402014-11-17 12:03:59 -0800268status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
269 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300270{
Dan Stozad3182402014-11-17 12:03:59 -0800271 if (rect.left < 0 || rect.right > width ||
272 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300273 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
274 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800275 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300276 return BAD_VALUE;
277 }
Dan Stozad3182402014-11-17 12:03:59 -0800278 status_t res = getBufferMapper().lockAsync(handle, inUsage, rect, vaddr,
279 fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300280 return res;
281}
282
Dan Stozad3182402014-11-17 12:03:59 -0800283status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
284 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300285{
286 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800287 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300288 return res;
289}
290
Dan Stozad3182402014-11-17 12:03:59 -0800291status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
292 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300293{
Dan Stozad3182402014-11-17 12:03:59 -0800294 if (rect.left < 0 || rect.right > width ||
295 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300296 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
297 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800298 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300299 return BAD_VALUE;
300 }
Dan Stozad3182402014-11-17 12:03:59 -0800301 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
302 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300303 return res;
304}
305
306status_t GraphicBuffer::unlockAsync(int *fenceFd)
307{
308 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
309 return res;
310}
311
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800312size_t GraphicBuffer::getFlattenedSize() const {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700313 return static_cast<size_t>(12 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800314}
Mathias Agopian3330b202009-10-05 17:07:12 -0700315
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800316size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800317 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800318}
319
Mathias Agopiane1424282013-07-29 21:24:40 -0700320status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800321 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
322 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700323
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800324 size_t fdCountNeeded = GraphicBuffer::getFdCount();
325 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700326
Dan Stozab1363d32014-03-28 15:10:52 -0700327 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800328 buf[0] = 'GBFR';
329 buf[1] = width;
330 buf[2] = height;
331 buf[3] = stride;
332 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700333 buf[5] = static_cast<int32_t>(layerCount);
334 buf[6] = usage;
335 buf[7] = static_cast<int32_t>(mId >> 32);
336 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
337 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700338 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700339 buf[11] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800340
341 if (handle) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700342 buf[10] = handle->numFds;
343 buf[11] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800344 memcpy(fds, handle->data,
345 static_cast<size_t>(handle->numFds) * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700346 memcpy(&buf[12], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800347 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700348 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800349
Dan Stozaeea6d682015-04-20 12:07:13 -0700350 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700351 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700352 if (handle) {
353 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800354 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700355 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700356
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800357 return NO_ERROR;
358}
359
Mathias Agopiane1424282013-07-29 21:24:40 -0700360status_t GraphicBuffer::unflatten(
361 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700362 if (size < 12 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800363
364 int const* buf = static_cast<int const*>(buffer);
365 if (buf[0] != 'GBFR') return BAD_TYPE;
366
Craig Donner6ebc46a2016-10-21 15:23:44 -0700367 const size_t numFds = static_cast<size_t>(buf[10]);
368 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800369
Michael Lentinec168b8a2015-02-18 10:14:18 -0800370 // Limit the maxNumber to be relatively small. The number of fds or ints
371 // should not come close to this number, and the number itself was simply
372 // chosen to be high enough to not cause issues and low enough to prevent
373 // overflow problems.
374 const size_t maxNumber = 4096;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700375 if (numFds >= maxNumber || numInts >= (maxNumber - 12)) {
376 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700377 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800378 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700379 numFds, numInts);
380 return BAD_VALUE;
381 }
382
Craig Donner6ebc46a2016-10-21 15:23:44 -0700383 const size_t sizeNeeded = (12 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800384 if (size < sizeNeeded) return NO_MEMORY;
385
Michael Lentine38803262014-10-31 15:25:03 -0700386 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800387 if (count < fdCountNeeded) return NO_MEMORY;
388
389 if (handle) {
390 // free previous handle if any
391 free_handle();
392 }
393
394 if (numFds || numInts) {
395 width = buf[1];
396 height = buf[2];
397 stride = buf[3];
398 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700399 layerCount = static_cast<uintptr_t>(buf[5]);
400 usage = buf[6];
Dan Stozad3182402014-11-17 12:03:59 -0800401 native_handle* h = native_handle_create(
402 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700403 if (!h) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700404 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700405 handle = NULL;
406 ALOGE("unflatten: native_handle_create failed");
407 return NO_MEMORY;
408 }
Dan Stozad3182402014-11-17 12:03:59 -0800409 memcpy(h->data, fds, numFds * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700410 memcpy(h->data + numFds, &buf[12], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800411 handle = h;
412 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700413 width = height = stride = format = layerCount = usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800414 handle = NULL;
415 }
416
Craig Donner6ebc46a2016-10-21 15:23:44 -0700417 mId = static_cast<uint64_t>(buf[7]) << 32;
418 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700419
Craig Donner6ebc46a2016-10-21 15:23:44 -0700420 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700421
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800422 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700423
424 if (handle != 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700425 status_t err = mBufferMapper.registerBuffer(this);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700426 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700427 width = height = stride = format = layerCount = usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800428 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700429 ALOGE("unflatten: registerBuffer failed: %s (%d)",
430 strerror(-err), err);
431 return err;
432 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700433 }
434
Dan Stozaeea6d682015-04-20 12:07:13 -0700435 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700436 size -= sizeNeeded;
437 fds += numFds;
438 count -= numFds;
439
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800440 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700441}
442
Mathias Agopian3330b202009-10-05 17:07:12 -0700443// ---------------------------------------------------------------------------
444
445}; // namespace android