blob: b54442667fcd899dce629b6563e8199cf1a9a82f [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;
Mathias Agopian841abed2017-02-10 16:15:34 -080054 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070055 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 =
Mathias Agopian3330b202009-10-05 17:07:12 -070067 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080068 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070069 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 =
Craig Donner6ebc46a2016-10-21 15:23:44 -070084 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080085 layerCount = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -070086 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
Daniel Nicoara1c457102017-02-07 17:27:25 -0500107GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
108 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inProducerUsage,
109 uint32_t inConsumerUsage, uint32_t inStride,
110 native_handle_t* inHandle, bool keepOwnership)
111 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
112 mBufferMapper(GraphicBufferMapper::get()),
113 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
114{
115 width = static_cast<int>(inWidth);
116 height = static_cast<int>(inHeight);
117 stride = static_cast<int>(inStride);
118 format = inFormat;
119 layerCount = inLayerCount;
120 usage = static_cast<int>(inConsumerUsage | inProducerUsage);
121 handle = inHandle;
122}
123
124
Iliyan Malchev697526b2011-05-01 11:33:26 -0700125GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700126 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
127 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -0700128 mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId()),
129 mGenerationNumber(0)
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700130{
131 width = buffer->width;
132 height = buffer->height;
133 stride = buffer->stride;
134 format = buffer->format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700135 layerCount = buffer->layerCount;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700136 usage = buffer->usage;
137 handle = buffer->handle;
138}
139
Mathias Agopian3330b202009-10-05 17:07:12 -0700140GraphicBuffer::~GraphicBuffer()
141{
142 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800143 free_handle();
144 }
145}
146
147void GraphicBuffer::free_handle()
148{
149 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700150 mBufferMapper.unregisterBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800151 if (!mBufferMapper.getGrallocMapper().valid()) {
152 native_handle_close(handle);
153 native_handle_delete(const_cast<native_handle*>(handle));
154 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800155 } else if (mOwner == ownData) {
156 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
157 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700158 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700159 handle = NULL;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700160 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700161}
162
163status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800164 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700165}
166
Mathias Agopian678bdd62010-12-03 17:33:09 -0800167void GraphicBuffer::dumpAllocationsToSystemLog()
168{
169 GraphicBufferAllocator::dumpToSystemLog();
170}
171
Iliyan Malchev697526b2011-05-01 11:33:26 -0700172ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700173{
Colin Cross18fae752014-07-22 15:55:08 -0700174 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700175 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700176 const_cast<GraphicBuffer*>(this));
177}
178
Dan Stozad3182402014-11-17 12:03:59 -0800179status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700180 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700181{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700182 if (mOwner != ownData)
183 return INVALID_OPERATION;
184
Dan Stozad3182402014-11-17 12:03:59 -0800185 if (handle &&
186 static_cast<int>(inWidth) == width &&
187 static_cast<int>(inHeight) == height &&
188 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700189 inLayerCount == layerCount &&
Dan Stozad3182402014-11-17 12:03:59 -0800190 static_cast<int>(inUsage) == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700191 return NO_ERROR;
192
Mathias Agopian3330b202009-10-05 17:07:12 -0700193 if (handle) {
194 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
195 allocator.free(handle);
196 handle = 0;
197 }
Craig Donnere96a3252017-02-02 12:13:34 -0800198 return initSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, inUsage,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700199 "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700200}
201
Dan Stoza9de72932015-04-16 17:28:43 -0700202bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700203 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700204{
205 if (static_cast<int>(inWidth) != width) return true;
206 if (static_cast<int>(inHeight) != height) return true;
207 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700208 if (inLayerCount != layerCount) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700209 if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
210 return false;
211}
212
Dan Stozad3182402014-11-17 12:03:59 -0800213status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
Craig Donnere96a3252017-02-02 12:13:34 -0800214 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inProducerUsage,
215 uint64_t inConsumerUsage, std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700216{
Mathias Agopian3330b202009-10-05 17:07:12 -0700217 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800218 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700219 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Craig Donnere96a3252017-02-02 12:13:34 -0800220 inProducerUsage, inConsumerUsage, &handle, &outStride, mId,
221 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700222 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800223 width = static_cast<int>(inWidth);
224 height = static_cast<int>(inHeight);
225 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700226 layerCount = inLayerCount;
Craig Donnere96a3252017-02-02 12:13:34 -0800227 usage = static_cast<int>(inProducerUsage | inConsumerUsage);
Dan Stozad3182402014-11-17 12:03:59 -0800228 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700229 }
230 return err;
231}
232
Dan Stozad3182402014-11-17 12:03:59 -0800233status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700234{
235 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800236 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700237 return res;
238}
239
Dan Stozad3182402014-11-17 12:03:59 -0800240status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700241{
Dan Stozad3182402014-11-17 12:03:59 -0800242 if (rect.left < 0 || rect.right > width ||
243 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000244 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800245 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800246 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700247 return BAD_VALUE;
248 }
Dan Stozad3182402014-11-17 12:03:59 -0800249 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700250 return res;
251}
252
Dan Stozad3182402014-11-17 12:03:59 -0800253status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700254{
255 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800256 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700257 return res;
258}
259
Dan Stozad3182402014-11-17 12:03:59 -0800260status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
261 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700262{
Dan Stozad3182402014-11-17 12:03:59 -0800263 if (rect.left < 0 || rect.right > width ||
264 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700265 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
266 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800267 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700268 return BAD_VALUE;
269 }
Dan Stozad3182402014-11-17 12:03:59 -0800270 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700271 return res;
272}
273
Mathias Agopian3330b202009-10-05 17:07:12 -0700274status_t GraphicBuffer::unlock()
275{
276 status_t res = getBufferMapper().unlock(handle);
277 return res;
278}
279
Dan Stozad3182402014-11-17 12:03:59 -0800280status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300281{
282 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800283 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300284 return res;
285}
286
Dan Stozad3182402014-11-17 12:03:59 -0800287status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
288 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300289{
Craig Donnere96a3252017-02-02 12:13:34 -0800290 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
291}
292
293status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
294 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
295{
Dan Stozad3182402014-11-17 12:03:59 -0800296 if (rect.left < 0 || rect.right > width ||
297 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300298 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
299 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800300 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300301 return BAD_VALUE;
302 }
Craig Donnere96a3252017-02-02 12:13:34 -0800303 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
304 inConsumerUsage, rect, vaddr, 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, android_ycbcr* ycbcr,
309 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300310{
311 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800312 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300313 return res;
314}
315
Dan Stozad3182402014-11-17 12:03:59 -0800316status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
317 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300318{
Dan Stozad3182402014-11-17 12:03:59 -0800319 if (rect.left < 0 || rect.right > width ||
320 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300321 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
322 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800323 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300324 return BAD_VALUE;
325 }
Dan Stozad3182402014-11-17 12:03:59 -0800326 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
327 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300328 return res;
329}
330
331status_t GraphicBuffer::unlockAsync(int *fenceFd)
332{
333 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
334 return res;
335}
336
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800337size_t GraphicBuffer::getFlattenedSize() const {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700338 return static_cast<size_t>(12 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800339}
Mathias Agopian3330b202009-10-05 17:07:12 -0700340
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800341size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800342 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800343}
344
Mathias Agopiane1424282013-07-29 21:24:40 -0700345status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800346 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
347 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700348
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800349 size_t fdCountNeeded = GraphicBuffer::getFdCount();
350 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700351
Dan Stozab1363d32014-03-28 15:10:52 -0700352 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800353 buf[0] = 'GBFR';
354 buf[1] = width;
355 buf[2] = height;
356 buf[3] = stride;
357 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700358 buf[5] = static_cast<int32_t>(layerCount);
359 buf[6] = usage;
360 buf[7] = static_cast<int32_t>(mId >> 32);
361 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
362 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700363 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700364 buf[11] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800365
366 if (handle) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700367 buf[10] = handle->numFds;
368 buf[11] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800369 memcpy(fds, handle->data,
370 static_cast<size_t>(handle->numFds) * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700371 memcpy(&buf[12], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800372 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700373 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800374
Dan Stozaeea6d682015-04-20 12:07:13 -0700375 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700376 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700377 if (handle) {
378 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800379 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700380 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700381
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800382 return NO_ERROR;
383}
384
Mathias Agopiane1424282013-07-29 21:24:40 -0700385status_t GraphicBuffer::unflatten(
386 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700387 if (size < 12 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800388
389 int const* buf = static_cast<int const*>(buffer);
390 if (buf[0] != 'GBFR') return BAD_TYPE;
391
Craig Donner6ebc46a2016-10-21 15:23:44 -0700392 const size_t numFds = static_cast<size_t>(buf[10]);
393 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800394
Michael Lentinec168b8a2015-02-18 10:14:18 -0800395 // Limit the maxNumber to be relatively small. The number of fds or ints
396 // should not come close to this number, and the number itself was simply
397 // chosen to be high enough to not cause issues and low enough to prevent
398 // overflow problems.
399 const size_t maxNumber = 4096;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700400 if (numFds >= maxNumber || numInts >= (maxNumber - 12)) {
401 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700402 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800403 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700404 numFds, numInts);
405 return BAD_VALUE;
406 }
407
Craig Donner6ebc46a2016-10-21 15:23:44 -0700408 const size_t sizeNeeded = (12 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800409 if (size < sizeNeeded) return NO_MEMORY;
410
Michael Lentine38803262014-10-31 15:25:03 -0700411 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800412 if (count < fdCountNeeded) return NO_MEMORY;
413
414 if (handle) {
415 // free previous handle if any
416 free_handle();
417 }
418
419 if (numFds || numInts) {
420 width = buf[1];
421 height = buf[2];
422 stride = buf[3];
423 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700424 layerCount = static_cast<uintptr_t>(buf[5]);
425 usage = buf[6];
Dan Stozad3182402014-11-17 12:03:59 -0800426 native_handle* h = native_handle_create(
427 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700428 if (!h) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700429 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700430 handle = NULL;
431 ALOGE("unflatten: native_handle_create failed");
432 return NO_MEMORY;
433 }
Dan Stozad3182402014-11-17 12:03:59 -0800434 memcpy(h->data, fds, numFds * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700435 memcpy(h->data + numFds, &buf[12], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800436 handle = h;
437 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700438 width = height = stride = format = layerCount = usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800439 handle = NULL;
440 }
441
Craig Donner6ebc46a2016-10-21 15:23:44 -0700442 mId = static_cast<uint64_t>(buf[7]) << 32;
443 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700444
Craig Donner6ebc46a2016-10-21 15:23:44 -0700445 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700446
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800447 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700448
449 if (handle != 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700450 status_t err = mBufferMapper.registerBuffer(this);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700451 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700452 width = height = stride = format = layerCount = usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800453 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700454 ALOGE("unflatten: registerBuffer failed: %s (%d)",
455 strerror(-err), err);
456 return err;
457 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700458 }
459
Dan Stozaeea6d682015-04-20 12:07:13 -0700460 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700461 size -= sizeNeeded;
462 fds += numFds;
463 count -= numFds;
464
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800465 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700466}
467
Mathias Agopian3330b202009-10-05 17:07:12 -0700468// ---------------------------------------------------------------------------
469
470}; // namespace android