blob: 37ebfb3cd4076ea18bad595751bce56c190dd666 [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 <ui/GraphicBuffer.h>
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080020#include <ui/GrallocMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070021#include <ui/GraphicBufferAllocator.h>
22#include <ui/GraphicBufferMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070023
Mathias Agopian3330b202009-10-05 17:07:12 -070024namespace android {
25
26// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070027// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070028// ===========================================================================
29
Dan Stozab1363d32014-03-28 15:10:52 -070030static uint64_t getUniqueId() {
31 static volatile int32_t nextId = 0;
32 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
33 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
34 return id;
35}
36
Mathias Agopian3330b202009-10-05 17:07:12 -070037GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070038 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070039 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070040{
Dan Stoza01049c82014-11-11 10:32:31 -080041 width =
42 height =
43 stride =
44 format =
Mathias Agopian3330b202009-10-05 17:07:12 -070045 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080046 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070047 handle = NULL;
48}
49
Dan Stozad3182402014-11-17 12:03:59 -080050GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070051 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070052 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070053 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian3330b202009-10-05 17:07:12 -070054{
Dan Stoza01049c82014-11-11 10:32:31 -080055 width =
56 height =
57 stride =
58 format =
Mathias Agopian3330b202009-10-05 17:07:12 -070059 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080060 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070061 handle = NULL;
Craig Donnere96a3252017-02-02 12:13:34 -080062 mInitCheck = initSize(inWidth, inHeight, inFormat, 1, inUsage, inUsage,
Dan Stoza024e9312016-08-24 12:17:29 -070063 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -070064}
65
Dan Stozad3182402014-11-17 12:03:59 -080066GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Craig Donnere96a3252017-02-02 12:13:34 -080067 PixelFormat inFormat, uint32_t inLayerCount, uint64_t producerUsage,
68 uint64_t consumerUsage, std::string requestorName)
Craig Donner6ebc46a2016-10-21 15:23:44 -070069 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
70 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
71{
72 width =
73 height =
74 stride =
75 format =
Craig Donner6ebc46a2016-10-21 15:23:44 -070076 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080077 layerCount = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -070078 handle = NULL;
Craig Donnere96a3252017-02-02 12:13:34 -080079 mInitCheck = initSize(inWidth, inHeight, inFormat, inLayerCount,
80 producerUsage, consumerUsage, std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070081}
82
83GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
84 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
85 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070086 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
87 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070088 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070089{
Dan Stozad3182402014-11-17 12:03:59 -080090 width = static_cast<int>(inWidth);
91 height = static_cast<int>(inHeight);
92 stride = static_cast<int>(inStride);
Mathias Agopian54ba51d2009-10-26 20:12:37 -070093 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -070094 layerCount = inLayerCount;
Dan Stozad3182402014-11-17 12:03:59 -080095 usage = static_cast<int>(inUsage);
Mathias Agopian54ba51d2009-10-26 20:12:37 -070096 handle = inHandle;
97}
98
Daniel Nicoara1c457102017-02-07 17:27:25 -050099GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
100 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inProducerUsage,
101 uint32_t inConsumerUsage, uint32_t inStride,
102 native_handle_t* inHandle, bool keepOwnership)
103 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
104 mBufferMapper(GraphicBufferMapper::get()),
105 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
106{
107 width = static_cast<int>(inWidth);
108 height = static_cast<int>(inHeight);
109 stride = static_cast<int>(inStride);
110 format = inFormat;
111 layerCount = inLayerCount;
112 usage = static_cast<int>(inConsumerUsage | inProducerUsage);
113 handle = inHandle;
114}
115
116
Iliyan Malchev697526b2011-05-01 11:33:26 -0700117GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700118 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
119 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -0700120 mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId()),
121 mGenerationNumber(0)
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700122{
123 width = buffer->width;
124 height = buffer->height;
125 stride = buffer->stride;
126 format = buffer->format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700127 layerCount = buffer->layerCount;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700128 usage = buffer->usage;
129 handle = buffer->handle;
130}
131
Mathias Agopian3330b202009-10-05 17:07:12 -0700132GraphicBuffer::~GraphicBuffer()
133{
134 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800135 free_handle();
136 }
137}
138
139void GraphicBuffer::free_handle()
140{
141 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700142 mBufferMapper.unregisterBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800143 if (!mBufferMapper.getGrallocMapper().valid()) {
144 native_handle_close(handle);
145 native_handle_delete(const_cast<native_handle*>(handle));
146 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800147 } else if (mOwner == ownData) {
148 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
149 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700150 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700151 handle = NULL;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700152 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700153}
154
155status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800156 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700157}
158
Mathias Agopian678bdd62010-12-03 17:33:09 -0800159void GraphicBuffer::dumpAllocationsToSystemLog()
160{
161 GraphicBufferAllocator::dumpToSystemLog();
162}
163
Iliyan Malchev697526b2011-05-01 11:33:26 -0700164ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700165{
Colin Cross18fae752014-07-22 15:55:08 -0700166 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700167 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700168 const_cast<GraphicBuffer*>(this));
169}
170
Dan Stozad3182402014-11-17 12:03:59 -0800171status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700172 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700173{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700174 if (mOwner != ownData)
175 return INVALID_OPERATION;
176
Dan Stozad3182402014-11-17 12:03:59 -0800177 if (handle &&
178 static_cast<int>(inWidth) == width &&
179 static_cast<int>(inHeight) == height &&
180 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700181 inLayerCount == layerCount &&
Dan Stozad3182402014-11-17 12:03:59 -0800182 static_cast<int>(inUsage) == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700183 return NO_ERROR;
184
Mathias Agopian3330b202009-10-05 17:07:12 -0700185 if (handle) {
186 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
187 allocator.free(handle);
188 handle = 0;
189 }
Craig Donnere96a3252017-02-02 12:13:34 -0800190 return initSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, inUsage,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700191 "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700192}
193
Dan Stoza9de72932015-04-16 17:28:43 -0700194bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700195 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700196{
197 if (static_cast<int>(inWidth) != width) return true;
198 if (static_cast<int>(inHeight) != height) return true;
199 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700200 if (inLayerCount != layerCount) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700201 if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
202 return false;
203}
204
Dan Stozad3182402014-11-17 12:03:59 -0800205status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
Craig Donnere96a3252017-02-02 12:13:34 -0800206 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inProducerUsage,
207 uint64_t inConsumerUsage, std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700208{
Mathias Agopian3330b202009-10-05 17:07:12 -0700209 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800210 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700211 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Craig Donnere96a3252017-02-02 12:13:34 -0800212 inProducerUsage, inConsumerUsage, &handle, &outStride, mId,
213 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700214 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800215 width = static_cast<int>(inWidth);
216 height = static_cast<int>(inHeight);
217 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700218 layerCount = inLayerCount;
Craig Donnere96a3252017-02-02 12:13:34 -0800219 usage = static_cast<int>(inProducerUsage | inConsumerUsage);
Dan Stozad3182402014-11-17 12:03:59 -0800220 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700221 }
222 return err;
223}
224
Dan Stozad3182402014-11-17 12:03:59 -0800225status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700226{
227 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800228 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700229 return res;
230}
231
Dan Stozad3182402014-11-17 12:03:59 -0800232status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700233{
Dan Stozad3182402014-11-17 12:03:59 -0800234 if (rect.left < 0 || rect.right > width ||
235 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000236 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800237 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800238 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700239 return BAD_VALUE;
240 }
Dan Stozad3182402014-11-17 12:03:59 -0800241 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700242 return res;
243}
244
Dan Stozad3182402014-11-17 12:03:59 -0800245status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700246{
247 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800248 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700249 return res;
250}
251
Dan Stozad3182402014-11-17 12:03:59 -0800252status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
253 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700254{
Dan Stozad3182402014-11-17 12:03:59 -0800255 if (rect.left < 0 || rect.right > width ||
256 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700257 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
258 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800259 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700260 return BAD_VALUE;
261 }
Dan Stozad3182402014-11-17 12:03:59 -0800262 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700263 return res;
264}
265
Mathias Agopian3330b202009-10-05 17:07:12 -0700266status_t GraphicBuffer::unlock()
267{
268 status_t res = getBufferMapper().unlock(handle);
269 return res;
270}
271
Dan Stozad3182402014-11-17 12:03:59 -0800272status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300273{
274 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800275 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300276 return res;
277}
278
Dan Stozad3182402014-11-17 12:03:59 -0800279status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
280 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300281{
Craig Donnere96a3252017-02-02 12:13:34 -0800282 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
283}
284
285status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
286 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
287{
Dan Stozad3182402014-11-17 12:03:59 -0800288 if (rect.left < 0 || rect.right > width ||
289 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300290 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
291 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800292 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300293 return BAD_VALUE;
294 }
Craig Donnere96a3252017-02-02 12:13:34 -0800295 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
296 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300297 return res;
298}
299
Dan Stozad3182402014-11-17 12:03:59 -0800300status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
301 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300302{
303 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800304 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300305 return res;
306}
307
Dan Stozad3182402014-11-17 12:03:59 -0800308status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
309 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300310{
Dan Stozad3182402014-11-17 12:03:59 -0800311 if (rect.left < 0 || rect.right > width ||
312 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300313 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
314 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800315 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300316 return BAD_VALUE;
317 }
Dan Stozad3182402014-11-17 12:03:59 -0800318 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
319 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300320 return res;
321}
322
323status_t GraphicBuffer::unlockAsync(int *fenceFd)
324{
325 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
326 return res;
327}
328
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800329size_t GraphicBuffer::getFlattenedSize() const {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700330 return static_cast<size_t>(12 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800331}
Mathias Agopian3330b202009-10-05 17:07:12 -0700332
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800333size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800334 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800335}
336
Mathias Agopiane1424282013-07-29 21:24:40 -0700337status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800338 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
339 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700340
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800341 size_t fdCountNeeded = GraphicBuffer::getFdCount();
342 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700343
Dan Stozab1363d32014-03-28 15:10:52 -0700344 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800345 buf[0] = 'GBFR';
346 buf[1] = width;
347 buf[2] = height;
348 buf[3] = stride;
349 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700350 buf[5] = static_cast<int32_t>(layerCount);
351 buf[6] = usage;
352 buf[7] = static_cast<int32_t>(mId >> 32);
353 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
354 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700355 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700356 buf[11] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800357
358 if (handle) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700359 buf[10] = handle->numFds;
360 buf[11] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800361 memcpy(fds, handle->data,
362 static_cast<size_t>(handle->numFds) * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700363 memcpy(&buf[12], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800364 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700365 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800366
Dan Stozaeea6d682015-04-20 12:07:13 -0700367 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700368 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700369 if (handle) {
370 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800371 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700372 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700373
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800374 return NO_ERROR;
375}
376
Mathias Agopiane1424282013-07-29 21:24:40 -0700377status_t GraphicBuffer::unflatten(
378 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700379 if (size < 12 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800380
381 int const* buf = static_cast<int const*>(buffer);
382 if (buf[0] != 'GBFR') return BAD_TYPE;
383
Craig Donner6ebc46a2016-10-21 15:23:44 -0700384 const size_t numFds = static_cast<size_t>(buf[10]);
385 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800386
Michael Lentinec168b8a2015-02-18 10:14:18 -0800387 // Limit the maxNumber to be relatively small. The number of fds or ints
388 // should not come close to this number, and the number itself was simply
389 // chosen to be high enough to not cause issues and low enough to prevent
390 // overflow problems.
391 const size_t maxNumber = 4096;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700392 if (numFds >= maxNumber || numInts >= (maxNumber - 12)) {
393 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700394 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800395 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700396 numFds, numInts);
397 return BAD_VALUE;
398 }
399
Craig Donner6ebc46a2016-10-21 15:23:44 -0700400 const size_t sizeNeeded = (12 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800401 if (size < sizeNeeded) return NO_MEMORY;
402
Michael Lentine38803262014-10-31 15:25:03 -0700403 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800404 if (count < fdCountNeeded) return NO_MEMORY;
405
406 if (handle) {
407 // free previous handle if any
408 free_handle();
409 }
410
411 if (numFds || numInts) {
412 width = buf[1];
413 height = buf[2];
414 stride = buf[3];
415 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700416 layerCount = static_cast<uintptr_t>(buf[5]);
417 usage = buf[6];
Dan Stozad3182402014-11-17 12:03:59 -0800418 native_handle* h = native_handle_create(
419 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700420 if (!h) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700421 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700422 handle = NULL;
423 ALOGE("unflatten: native_handle_create failed");
424 return NO_MEMORY;
425 }
Dan Stozad3182402014-11-17 12:03:59 -0800426 memcpy(h->data, fds, numFds * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700427 memcpy(h->data + numFds, &buf[12], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800428 handle = h;
429 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700430 width = height = stride = format = layerCount = usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800431 handle = NULL;
432 }
433
Craig Donner6ebc46a2016-10-21 15:23:44 -0700434 mId = static_cast<uint64_t>(buf[7]) << 32;
435 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700436
Craig Donner6ebc46a2016-10-21 15:23:44 -0700437 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700438
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800439 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700440
441 if (handle != 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700442 status_t err = mBufferMapper.registerBuffer(this);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700443 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700444 width = height = stride = format = layerCount = usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800445 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700446 ALOGE("unflatten: registerBuffer failed: %s (%d)",
447 strerror(-err), err);
448 return err;
449 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700450 }
451
Dan Stozaeea6d682015-04-20 12:07:13 -0700452 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700453 size -= sizeNeeded;
454 fds += numFds;
455 count -= numFds;
456
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800457 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700458}
459
Mathias Agopian3330b202009-10-05 17:07:12 -0700460// ---------------------------------------------------------------------------
461
462}; // namespace android