blob: d21758d41e52e9be0ace415082b6558c5a0d938f [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 Agopian311b4792017-02-28 15:00:49 -080020
21#include <cutils/atomic.h>
22
Jesse Hall79927812017-03-23 11:03:23 -070023#include <grallocusage/GrallocUsageConversion.h>
24
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080025#include <ui/GrallocMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070026#include <ui/GraphicBufferAllocator.h>
27#include <ui/GraphicBufferMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070028
Mathias Agopian3330b202009-10-05 17:07:12 -070029namespace android {
30
31// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070032// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070033// ===========================================================================
34
Dan Stozab1363d32014-03-28 15:10:52 -070035static uint64_t getUniqueId() {
36 static volatile int32_t nextId = 0;
37 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
38 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
39 return id;
40}
41
Mathias Agopianf543e5a2017-04-03 17:16:41 -070042sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
43 return static_cast<GraphicBuffer *>(anwb);
44}
45
Mathias Agopian3330b202009-10-05 17:07:12 -070046GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070047 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070048 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070049{
Dan Stoza01049c82014-11-11 10:32:31 -080050 width =
51 height =
52 stride =
53 format =
Mathias Agopian3330b202009-10-05 17:07:12 -070054 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080055 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070056 handle = NULL;
57}
58
Chia-I Wub42f1712017-03-21 13:15:39 -070059// deprecated
Dan Stozad3182402014-11-17 12:03:59 -080060GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070061 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Chia-I Wub42f1712017-03-21 13:15:39 -070062 : GraphicBuffer(inWidth, inHeight, inFormat, 1, inUsage, inUsage,
63 requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -070064{
Mathias Agopian3330b202009-10-05 17:07:12 -070065}
66
Dan Stozad3182402014-11-17 12:03:59 -080067GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Craig Donnere96a3252017-02-02 12:13:34 -080068 PixelFormat inFormat, uint32_t inLayerCount, uint64_t producerUsage,
69 uint64_t consumerUsage, std::string requestorName)
Chia-I Wub42f1712017-03-21 13:15:39 -070070 : GraphicBuffer()
Craig Donner6ebc46a2016-10-21 15:23:44 -070071{
Chia-I Wub42f1712017-03-21 13:15:39 -070072 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount,
Craig Donnere96a3252017-02-02 12:13:34 -080073 producerUsage, consumerUsage, std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070074}
75
Chia-I Wub42f1712017-03-21 13:15:39 -070076// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -070077GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
78 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
79 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -070080 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
81 inWidth, inHeight, inFormat, inLayerCount, inUsage, inUsage,
82 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070083{
Mathias Agopian54ba51d2009-10-26 20:12:37 -070084}
85
Chia-I Wub42f1712017-03-21 13:15:39 -070086GraphicBuffer::GraphicBuffer(const native_handle_t* handle,
87 HandleWrapMethod method, uint32_t width, uint32_t height,
88 PixelFormat format, uint32_t layerCount,
89 uint64_t producerUsage, uint64_t consumerUsage,
90 uint32_t stride)
91 : GraphicBuffer()
92{
93 mInitCheck = initWithHandle(handle, method, width, height, format,
94 layerCount, producerUsage, consumerUsage, stride);
95}
96
Mathias Agopian3330b202009-10-05 17:07:12 -070097GraphicBuffer::~GraphicBuffer()
98{
99 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800100 free_handle();
101 }
102}
103
104void GraphicBuffer::free_handle()
105{
106 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700107 mBufferMapper.unregisterBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800108 if (!mBufferMapper.getGrallocMapper().valid()) {
109 native_handle_close(handle);
110 native_handle_delete(const_cast<native_handle*>(handle));
111 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800112 } else if (mOwner == ownData) {
113 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
114 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700115 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700116 handle = NULL;
Mathias Agopian3330b202009-10-05 17:07:12 -0700117}
118
119status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800120 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700121}
122
Mathias Agopian678bdd62010-12-03 17:33:09 -0800123void GraphicBuffer::dumpAllocationsToSystemLog()
124{
125 GraphicBufferAllocator::dumpToSystemLog();
126}
127
Iliyan Malchev697526b2011-05-01 11:33:26 -0700128ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700129{
Colin Cross18fae752014-07-22 15:55:08 -0700130 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700131 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700132 const_cast<GraphicBuffer*>(this));
133}
134
Dan Stozad3182402014-11-17 12:03:59 -0800135status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700136 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700137{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700138 if (mOwner != ownData)
139 return INVALID_OPERATION;
140
Dan Stozad3182402014-11-17 12:03:59 -0800141 if (handle &&
142 static_cast<int>(inWidth) == width &&
143 static_cast<int>(inHeight) == height &&
144 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700145 inLayerCount == layerCount &&
Dan Stozad3182402014-11-17 12:03:59 -0800146 static_cast<int>(inUsage) == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700147 return NO_ERROR;
148
Mathias Agopian3330b202009-10-05 17:07:12 -0700149 if (handle) {
150 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
151 allocator.free(handle);
152 handle = 0;
153 }
Chia-I Wub42f1712017-03-21 13:15:39 -0700154 return initWithSize(inWidth, inHeight, inFormat, inLayerCount,
155 inUsage, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700156}
157
Dan Stoza9de72932015-04-16 17:28:43 -0700158bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700159 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700160{
161 if (static_cast<int>(inWidth) != width) return true;
162 if (static_cast<int>(inHeight) != height) return true;
163 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700164 if (inLayerCount != layerCount) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700165 if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
166 return false;
167}
168
Chia-I Wub42f1712017-03-21 13:15:39 -0700169status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Craig Donnere96a3252017-02-02 12:13:34 -0800170 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inProducerUsage,
171 uint64_t inConsumerUsage, std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700172{
Mathias Agopian3330b202009-10-05 17:07:12 -0700173 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800174 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700175 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Craig Donnere96a3252017-02-02 12:13:34 -0800176 inProducerUsage, inConsumerUsage, &handle, &outStride, mId,
177 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700178 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800179 width = static_cast<int>(inWidth);
180 height = static_cast<int>(inHeight);
181 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700182 layerCount = inLayerCount;
Jesse Hall79927812017-03-23 11:03:23 -0700183 usage = android_convertGralloc1To0Usage(inProducerUsage, inConsumerUsage);
Dan Stozad3182402014-11-17 12:03:59 -0800184 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700185 }
186 return err;
187}
188
Chia-I Wub42f1712017-03-21 13:15:39 -0700189status_t GraphicBuffer::initWithHandle(const native_handle_t* handle,
190 HandleWrapMethod method, uint32_t width, uint32_t height,
191 PixelFormat format, uint32_t layerCount,
192 uint64_t producerUsage, uint64_t consumerUsage,
193 uint32_t stride)
194{
195 native_handle_t* clone = nullptr;
196
197 if (method == CLONE_HANDLE) {
198 clone = native_handle_clone(handle);
199 if (!clone) {
200 return NO_MEMORY;
201 }
202
203 handle = clone;
204 method = TAKE_UNREGISTERED_HANDLE;
205 }
206
207 ANativeWindowBuffer::width = static_cast<int>(width);
208 ANativeWindowBuffer::height = static_cast<int>(height);
209 ANativeWindowBuffer::stride = static_cast<int>(stride);
210 ANativeWindowBuffer::format = format;
211 ANativeWindowBuffer::usage =
212 android_convertGralloc1To0Usage(producerUsage, consumerUsage);
213
214 ANativeWindowBuffer::layerCount = layerCount;
215 ANativeWindowBuffer::handle = handle;
216
217 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
218
219 if (method == TAKE_UNREGISTERED_HANDLE) {
220 status_t err = mBufferMapper.registerBuffer(this);
221 if (err != NO_ERROR) {
222 // clean up cloned handle
223 if (clone) {
224 native_handle_close(clone);
225 native_handle_delete(clone);
226 }
227
228 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0, 0);
229
230 return err;
231 }
232 }
233
234 return NO_ERROR;
235}
236
Dan Stozad3182402014-11-17 12:03:59 -0800237status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700238{
239 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800240 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700241 return res;
242}
243
Dan Stozad3182402014-11-17 12:03:59 -0800244status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700245{
Dan Stozad3182402014-11-17 12:03:59 -0800246 if (rect.left < 0 || rect.right > width ||
247 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000248 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800249 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800250 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700251 return BAD_VALUE;
252 }
Dan Stozad3182402014-11-17 12:03:59 -0800253 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700254 return res;
255}
256
Dan Stozad3182402014-11-17 12:03:59 -0800257status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700258{
259 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800260 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700261 return res;
262}
263
Dan Stozad3182402014-11-17 12:03:59 -0800264status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
265 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700266{
Dan Stozad3182402014-11-17 12:03:59 -0800267 if (rect.left < 0 || rect.right > width ||
268 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700269 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
270 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800271 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700272 return BAD_VALUE;
273 }
Dan Stozad3182402014-11-17 12:03:59 -0800274 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700275 return res;
276}
277
Mathias Agopian3330b202009-10-05 17:07:12 -0700278status_t GraphicBuffer::unlock()
279{
280 status_t res = getBufferMapper().unlock(handle);
281 return res;
282}
283
Dan Stozad3182402014-11-17 12:03:59 -0800284status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, 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 = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300288 return res;
289}
290
Dan Stozad3182402014-11-17 12:03:59 -0800291status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
292 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300293{
Craig Donnere96a3252017-02-02 12:13:34 -0800294 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
295}
296
297status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
298 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
299{
Dan Stozad3182402014-11-17 12:03:59 -0800300 if (rect.left < 0 || rect.right > width ||
301 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300302 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
303 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800304 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300305 return BAD_VALUE;
306 }
Craig Donnere96a3252017-02-02 12:13:34 -0800307 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
308 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300309 return res;
310}
311
Dan Stozad3182402014-11-17 12:03:59 -0800312status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
313 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300314{
315 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800316 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300317 return res;
318}
319
Dan Stozad3182402014-11-17 12:03:59 -0800320status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
321 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300322{
Dan Stozad3182402014-11-17 12:03:59 -0800323 if (rect.left < 0 || rect.right > width ||
324 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300325 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
326 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800327 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300328 return BAD_VALUE;
329 }
Dan Stozad3182402014-11-17 12:03:59 -0800330 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
331 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300332 return res;
333}
334
335status_t GraphicBuffer::unlockAsync(int *fenceFd)
336{
337 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
338 return res;
339}
340
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800341size_t GraphicBuffer::getFlattenedSize() const {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700342 return static_cast<size_t>(12 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800343}
Mathias Agopian3330b202009-10-05 17:07:12 -0700344
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800345size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800346 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800347}
348
Mathias Agopiane1424282013-07-29 21:24:40 -0700349status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800350 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
351 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700352
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800353 size_t fdCountNeeded = GraphicBuffer::getFdCount();
354 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700355
Dan Stozab1363d32014-03-28 15:10:52 -0700356 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800357 buf[0] = 'GBFR';
358 buf[1] = width;
359 buf[2] = height;
360 buf[3] = stride;
361 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700362 buf[5] = static_cast<int32_t>(layerCount);
363 buf[6] = usage;
364 buf[7] = static_cast<int32_t>(mId >> 32);
365 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
366 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700367 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700368 buf[11] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800369
370 if (handle) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700371 buf[10] = handle->numFds;
372 buf[11] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800373 memcpy(fds, handle->data,
374 static_cast<size_t>(handle->numFds) * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700375 memcpy(&buf[12], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800376 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700377 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800378
Dan Stozaeea6d682015-04-20 12:07:13 -0700379 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700380 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700381 if (handle) {
382 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800383 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700384 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700385
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800386 return NO_ERROR;
387}
388
Mathias Agopiane1424282013-07-29 21:24:40 -0700389status_t GraphicBuffer::unflatten(
390 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700391 if (size < 12 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800392
393 int const* buf = static_cast<int const*>(buffer);
394 if (buf[0] != 'GBFR') return BAD_TYPE;
395
Craig Donner6ebc46a2016-10-21 15:23:44 -0700396 const size_t numFds = static_cast<size_t>(buf[10]);
397 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800398
Michael Lentinec168b8a2015-02-18 10:14:18 -0800399 // Limit the maxNumber to be relatively small. The number of fds or ints
400 // should not come close to this number, and the number itself was simply
401 // chosen to be high enough to not cause issues and low enough to prevent
402 // overflow problems.
403 const size_t maxNumber = 4096;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700404 if (numFds >= maxNumber || numInts >= (maxNumber - 12)) {
405 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700406 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800407 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700408 numFds, numInts);
409 return BAD_VALUE;
410 }
411
Craig Donner6ebc46a2016-10-21 15:23:44 -0700412 const size_t sizeNeeded = (12 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800413 if (size < sizeNeeded) return NO_MEMORY;
414
Michael Lentine38803262014-10-31 15:25:03 -0700415 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800416 if (count < fdCountNeeded) return NO_MEMORY;
417
418 if (handle) {
419 // free previous handle if any
420 free_handle();
421 }
422
423 if (numFds || numInts) {
424 width = buf[1];
425 height = buf[2];
426 stride = buf[3];
427 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700428 layerCount = static_cast<uintptr_t>(buf[5]);
429 usage = buf[6];
Dan Stozad3182402014-11-17 12:03:59 -0800430 native_handle* h = native_handle_create(
431 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700432 if (!h) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700433 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700434 handle = NULL;
435 ALOGE("unflatten: native_handle_create failed");
436 return NO_MEMORY;
437 }
Dan Stozad3182402014-11-17 12:03:59 -0800438 memcpy(h->data, fds, numFds * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700439 memcpy(h->data + numFds, &buf[12], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800440 handle = h;
441 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700442 width = height = stride = format = layerCount = usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800443 handle = NULL;
444 }
445
Craig Donner6ebc46a2016-10-21 15:23:44 -0700446 mId = static_cast<uint64_t>(buf[7]) << 32;
447 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700448
Craig Donner6ebc46a2016-10-21 15:23:44 -0700449 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700450
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800451 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700452
453 if (handle != 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700454 status_t err = mBufferMapper.registerBuffer(this);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700455 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700456 width = height = stride = format = layerCount = usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800457 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700458 ALOGE("unflatten: registerBuffer failed: %s (%d)",
459 strerror(-err), err);
460 return err;
461 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700462 }
463
Dan Stozaeea6d682015-04-20 12:07:13 -0700464 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700465 size -= sizeNeeded;
466 fds += numFds;
467 count -= numFds;
468
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800469 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700470}
471
Mathias Agopian3330b202009-10-05 17:07:12 -0700472// ---------------------------------------------------------------------------
473
474}; // namespace android