blob: d29bae177b3bd415473bda63084088fb9931318d [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 Donnere96a3252017-02-02 12:13:34 -080070 mInitCheck = initSize(inWidth, inHeight, inFormat, 1, inUsage, 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 Donnere96a3252017-02-02 12:13:34 -080075 PixelFormat inFormat, uint32_t inLayerCount, uint64_t producerUsage,
76 uint64_t consumerUsage, std::string requestorName)
Craig Donner6ebc46a2016-10-21 15:23:44 -070077 : 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;
Craig Donnere96a3252017-02-02 12:13:34 -080087 mInitCheck = initSize(inWidth, inHeight, inFormat, inLayerCount,
88 producerUsage, consumerUsage, std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070089}
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 Donnere96a3252017-02-02 12:13:34 -0800180 return initSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, inUsage,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700181 "[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 Donnere96a3252017-02-02 12:13:34 -0800196 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inProducerUsage,
197 uint64_t inConsumerUsage, 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,
Craig Donnere96a3252017-02-02 12:13:34 -0800202 inProducerUsage, inConsumerUsage, &handle, &outStride, mId,
203 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700204 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800205 width = static_cast<int>(inWidth);
206 height = static_cast<int>(inHeight);
207 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700208 layerCount = inLayerCount;
Craig Donnere96a3252017-02-02 12:13:34 -0800209 usage = static_cast<int>(inProducerUsage | inConsumerUsage);
Dan Stozad3182402014-11-17 12:03:59 -0800210 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700211 }
212 return err;
213}
214
Dan Stozad3182402014-11-17 12:03:59 -0800215status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700216{
217 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800218 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700219 return res;
220}
221
Dan Stozad3182402014-11-17 12:03:59 -0800222status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700223{
Dan Stozad3182402014-11-17 12:03:59 -0800224 if (rect.left < 0 || rect.right > width ||
225 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000226 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800227 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800228 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700229 return BAD_VALUE;
230 }
Dan Stozad3182402014-11-17 12:03:59 -0800231 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700232 return res;
233}
234
Dan Stozad3182402014-11-17 12:03:59 -0800235status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700236{
237 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800238 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700239 return res;
240}
241
Dan Stozad3182402014-11-17 12:03:59 -0800242status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
243 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700244{
Dan Stozad3182402014-11-17 12:03:59 -0800245 if (rect.left < 0 || rect.right > width ||
246 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700247 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);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700250 return BAD_VALUE;
251 }
Dan Stozad3182402014-11-17 12:03:59 -0800252 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700253 return res;
254}
255
Mathias Agopian3330b202009-10-05 17:07:12 -0700256status_t GraphicBuffer::unlock()
257{
258 status_t res = getBufferMapper().unlock(handle);
259 return res;
260}
261
Dan Stozad3182402014-11-17 12:03:59 -0800262status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300263{
264 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800265 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300266 return res;
267}
268
Dan Stozad3182402014-11-17 12:03:59 -0800269status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
270 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300271{
Craig Donnere96a3252017-02-02 12:13:34 -0800272 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
273}
274
275status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
276 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
277{
Dan Stozad3182402014-11-17 12:03:59 -0800278 if (rect.left < 0 || rect.right > width ||
279 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300280 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
281 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800282 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300283 return BAD_VALUE;
284 }
Craig Donnere96a3252017-02-02 12:13:34 -0800285 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
286 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300287 return res;
288}
289
Dan Stozad3182402014-11-17 12:03:59 -0800290status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
291 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300292{
293 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800294 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300295 return res;
296}
297
Dan Stozad3182402014-11-17 12:03:59 -0800298status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
299 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300300{
Dan Stozad3182402014-11-17 12:03:59 -0800301 if (rect.left < 0 || rect.right > width ||
302 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300303 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
304 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800305 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300306 return BAD_VALUE;
307 }
Dan Stozad3182402014-11-17 12:03:59 -0800308 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
309 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300310 return res;
311}
312
313status_t GraphicBuffer::unlockAsync(int *fenceFd)
314{
315 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
316 return res;
317}
318
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800319size_t GraphicBuffer::getFlattenedSize() const {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700320 return static_cast<size_t>(12 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800321}
Mathias Agopian3330b202009-10-05 17:07:12 -0700322
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800323size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800324 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800325}
326
Mathias Agopiane1424282013-07-29 21:24:40 -0700327status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800328 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
329 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700330
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800331 size_t fdCountNeeded = GraphicBuffer::getFdCount();
332 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700333
Dan Stozab1363d32014-03-28 15:10:52 -0700334 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800335 buf[0] = 'GBFR';
336 buf[1] = width;
337 buf[2] = height;
338 buf[3] = stride;
339 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700340 buf[5] = static_cast<int32_t>(layerCount);
341 buf[6] = usage;
342 buf[7] = static_cast<int32_t>(mId >> 32);
343 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
344 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700345 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700346 buf[11] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800347
348 if (handle) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700349 buf[10] = handle->numFds;
350 buf[11] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800351 memcpy(fds, handle->data,
352 static_cast<size_t>(handle->numFds) * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700353 memcpy(&buf[12], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800354 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700355 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800356
Dan Stozaeea6d682015-04-20 12:07:13 -0700357 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700358 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700359 if (handle) {
360 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800361 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700362 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700363
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800364 return NO_ERROR;
365}
366
Mathias Agopiane1424282013-07-29 21:24:40 -0700367status_t GraphicBuffer::unflatten(
368 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700369 if (size < 12 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800370
371 int const* buf = static_cast<int const*>(buffer);
372 if (buf[0] != 'GBFR') return BAD_TYPE;
373
Craig Donner6ebc46a2016-10-21 15:23:44 -0700374 const size_t numFds = static_cast<size_t>(buf[10]);
375 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800376
Michael Lentinec168b8a2015-02-18 10:14:18 -0800377 // Limit the maxNumber to be relatively small. The number of fds or ints
378 // should not come close to this number, and the number itself was simply
379 // chosen to be high enough to not cause issues and low enough to prevent
380 // overflow problems.
381 const size_t maxNumber = 4096;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700382 if (numFds >= maxNumber || numInts >= (maxNumber - 12)) {
383 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700384 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800385 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700386 numFds, numInts);
387 return BAD_VALUE;
388 }
389
Craig Donner6ebc46a2016-10-21 15:23:44 -0700390 const size_t sizeNeeded = (12 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800391 if (size < sizeNeeded) return NO_MEMORY;
392
Michael Lentine38803262014-10-31 15:25:03 -0700393 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800394 if (count < fdCountNeeded) return NO_MEMORY;
395
396 if (handle) {
397 // free previous handle if any
398 free_handle();
399 }
400
401 if (numFds || numInts) {
402 width = buf[1];
403 height = buf[2];
404 stride = buf[3];
405 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700406 layerCount = static_cast<uintptr_t>(buf[5]);
407 usage = buf[6];
Dan Stozad3182402014-11-17 12:03:59 -0800408 native_handle* h = native_handle_create(
409 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700410 if (!h) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700411 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700412 handle = NULL;
413 ALOGE("unflatten: native_handle_create failed");
414 return NO_MEMORY;
415 }
Dan Stozad3182402014-11-17 12:03:59 -0800416 memcpy(h->data, fds, numFds * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700417 memcpy(h->data + numFds, &buf[12], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800418 handle = h;
419 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700420 width = height = stride = format = layerCount = usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800421 handle = NULL;
422 }
423
Craig Donner6ebc46a2016-10-21 15:23:44 -0700424 mId = static_cast<uint64_t>(buf[7]) << 32;
425 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700426
Craig Donner6ebc46a2016-10-21 15:23:44 -0700427 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700428
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800429 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700430
431 if (handle != 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700432 status_t err = mBufferMapper.registerBuffer(this);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700433 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700434 width = height = stride = format = layerCount = usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800435 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700436 ALOGE("unflatten: registerBuffer failed: %s (%d)",
437 strerror(-err), err);
438 return err;
439 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700440 }
441
Dan Stozaeea6d682015-04-20 12:07:13 -0700442 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700443 size -= sizeNeeded;
444 fds += numFds;
445 count -= numFds;
446
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800447 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700448}
449
Mathias Agopian3330b202009-10-05 17:07:12 -0700450// ---------------------------------------------------------------------------
451
452}; // namespace android