blob: 4fae2337d4f16a05ac4bde1fe9f8bfa112cd76e6 [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 Agopian3330b202009-10-05 17:07:12 -070042GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070043 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070044 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070045{
Dan Stoza01049c82014-11-11 10:32:31 -080046 width =
47 height =
48 stride =
49 format =
Mathias Agopian3330b202009-10-05 17:07:12 -070050 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080051 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070052 handle = NULL;
53}
54
Chia-I Wub42f1712017-03-21 13:15:39 -070055// deprecated
Dan Stozad3182402014-11-17 12:03:59 -080056GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070057 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Chia-I Wub42f1712017-03-21 13:15:39 -070058 : GraphicBuffer(inWidth, inHeight, inFormat, 1, inUsage, inUsage,
59 requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -070060{
Mathias Agopian3330b202009-10-05 17:07:12 -070061}
62
Dan Stozad3182402014-11-17 12:03:59 -080063GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Craig Donnere96a3252017-02-02 12:13:34 -080064 PixelFormat inFormat, uint32_t inLayerCount, uint64_t producerUsage,
65 uint64_t consumerUsage, std::string requestorName)
Chia-I Wub42f1712017-03-21 13:15:39 -070066 : GraphicBuffer()
Craig Donner6ebc46a2016-10-21 15:23:44 -070067{
Chia-I Wub42f1712017-03-21 13:15:39 -070068 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount,
Craig Donnere96a3252017-02-02 12:13:34 -080069 producerUsage, consumerUsage, std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070070}
71
Chia-I Wub42f1712017-03-21 13:15:39 -070072// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -070073GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
74 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
75 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -070076 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
77 inWidth, inHeight, inFormat, inLayerCount, inUsage, inUsage,
78 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070079{
Mathias Agopian54ba51d2009-10-26 20:12:37 -070080}
81
Iliyan Malchev697526b2011-05-01 11:33:26 -070082GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070083 : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
84 mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070085 mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId()),
86 mGenerationNumber(0)
Jamie Gennis309d3bb2010-10-07 13:46:55 -070087{
88 width = buffer->width;
89 height = buffer->height;
90 stride = buffer->stride;
91 format = buffer->format;
Craig Donner6ebc46a2016-10-21 15:23:44 -070092 layerCount = buffer->layerCount;
Jamie Gennis309d3bb2010-10-07 13:46:55 -070093 usage = buffer->usage;
94 handle = buffer->handle;
95}
96
Chia-I Wub42f1712017-03-21 13:15:39 -070097GraphicBuffer::GraphicBuffer(const native_handle_t* handle,
98 HandleWrapMethod method, uint32_t width, uint32_t height,
99 PixelFormat format, uint32_t layerCount,
100 uint64_t producerUsage, uint64_t consumerUsage,
101 uint32_t stride)
102 : GraphicBuffer()
103{
104 mInitCheck = initWithHandle(handle, method, width, height, format,
105 layerCount, producerUsage, consumerUsage, stride);
106}
107
Mathias Agopian3330b202009-10-05 17:07:12 -0700108GraphicBuffer::~GraphicBuffer()
109{
110 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800111 free_handle();
112 }
113}
114
115void GraphicBuffer::free_handle()
116{
117 if (mOwner == ownHandle) {
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700118 mBufferMapper.unregisterBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800119 if (!mBufferMapper.getGrallocMapper().valid()) {
120 native_handle_close(handle);
121 native_handle_delete(const_cast<native_handle*>(handle));
122 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800123 } else if (mOwner == ownData) {
124 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
125 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700126 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700127 handle = NULL;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700128 mWrappedBuffer = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -0700129}
130
131status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800132 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700133}
134
Mathias Agopian678bdd62010-12-03 17:33:09 -0800135void GraphicBuffer::dumpAllocationsToSystemLog()
136{
137 GraphicBufferAllocator::dumpToSystemLog();
138}
139
Iliyan Malchev697526b2011-05-01 11:33:26 -0700140ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700141{
Colin Cross18fae752014-07-22 15:55:08 -0700142 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700143 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700144 const_cast<GraphicBuffer*>(this));
145}
146
Dan Stozad3182402014-11-17 12:03:59 -0800147status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700148 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700149{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700150 if (mOwner != ownData)
151 return INVALID_OPERATION;
152
Dan Stozad3182402014-11-17 12:03:59 -0800153 if (handle &&
154 static_cast<int>(inWidth) == width &&
155 static_cast<int>(inHeight) == height &&
156 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700157 inLayerCount == layerCount &&
Dan Stozad3182402014-11-17 12:03:59 -0800158 static_cast<int>(inUsage) == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700159 return NO_ERROR;
160
Mathias Agopian3330b202009-10-05 17:07:12 -0700161 if (handle) {
162 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
163 allocator.free(handle);
164 handle = 0;
165 }
Chia-I Wub42f1712017-03-21 13:15:39 -0700166 return initWithSize(inWidth, inHeight, inFormat, inLayerCount,
167 inUsage, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700168}
169
Dan Stoza9de72932015-04-16 17:28:43 -0700170bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700171 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700172{
173 if (static_cast<int>(inWidth) != width) return true;
174 if (static_cast<int>(inHeight) != height) return true;
175 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700176 if (inLayerCount != layerCount) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700177 if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
178 return false;
179}
180
Chia-I Wub42f1712017-03-21 13:15:39 -0700181status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Craig Donnere96a3252017-02-02 12:13:34 -0800182 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inProducerUsage,
183 uint64_t inConsumerUsage, std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700184{
Mathias Agopian3330b202009-10-05 17:07:12 -0700185 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800186 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700187 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Craig Donnere96a3252017-02-02 12:13:34 -0800188 inProducerUsage, inConsumerUsage, &handle, &outStride, mId,
189 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700190 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800191 width = static_cast<int>(inWidth);
192 height = static_cast<int>(inHeight);
193 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700194 layerCount = inLayerCount;
Jesse Hall79927812017-03-23 11:03:23 -0700195 usage = android_convertGralloc1To0Usage(inProducerUsage, inConsumerUsage);
Dan Stozad3182402014-11-17 12:03:59 -0800196 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700197 }
198 return err;
199}
200
Chia-I Wub42f1712017-03-21 13:15:39 -0700201status_t GraphicBuffer::initWithHandle(const native_handle_t* handle,
202 HandleWrapMethod method, uint32_t width, uint32_t height,
203 PixelFormat format, uint32_t layerCount,
204 uint64_t producerUsage, uint64_t consumerUsage,
205 uint32_t stride)
206{
207 native_handle_t* clone = nullptr;
208
209 if (method == CLONE_HANDLE) {
210 clone = native_handle_clone(handle);
211 if (!clone) {
212 return NO_MEMORY;
213 }
214
215 handle = clone;
216 method = TAKE_UNREGISTERED_HANDLE;
217 }
218
219 ANativeWindowBuffer::width = static_cast<int>(width);
220 ANativeWindowBuffer::height = static_cast<int>(height);
221 ANativeWindowBuffer::stride = static_cast<int>(stride);
222 ANativeWindowBuffer::format = format;
223 ANativeWindowBuffer::usage =
224 android_convertGralloc1To0Usage(producerUsage, consumerUsage);
225
226 ANativeWindowBuffer::layerCount = layerCount;
227 ANativeWindowBuffer::handle = handle;
228
229 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
230
231 if (method == TAKE_UNREGISTERED_HANDLE) {
232 status_t err = mBufferMapper.registerBuffer(this);
233 if (err != NO_ERROR) {
234 // clean up cloned handle
235 if (clone) {
236 native_handle_close(clone);
237 native_handle_delete(clone);
238 }
239
240 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0, 0);
241
242 return err;
243 }
244 }
245
246 return NO_ERROR;
247}
248
Dan Stozad3182402014-11-17 12:03:59 -0800249status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700250{
251 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800252 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700253 return res;
254}
255
Dan Stozad3182402014-11-17 12:03:59 -0800256status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700257{
Dan Stozad3182402014-11-17 12:03:59 -0800258 if (rect.left < 0 || rect.right > width ||
259 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000260 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800261 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800262 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700263 return BAD_VALUE;
264 }
Dan Stozad3182402014-11-17 12:03:59 -0800265 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700266 return res;
267}
268
Dan Stozad3182402014-11-17 12:03:59 -0800269status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700270{
271 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800272 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700273 return res;
274}
275
Dan Stozad3182402014-11-17 12:03:59 -0800276status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
277 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700278{
Dan Stozad3182402014-11-17 12:03:59 -0800279 if (rect.left < 0 || rect.right > width ||
280 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700281 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
282 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800283 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700284 return BAD_VALUE;
285 }
Dan Stozad3182402014-11-17 12:03:59 -0800286 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700287 return res;
288}
289
Mathias Agopian3330b202009-10-05 17:07:12 -0700290status_t GraphicBuffer::unlock()
291{
292 status_t res = getBufferMapper().unlock(handle);
293 return res;
294}
295
Dan Stozad3182402014-11-17 12:03:59 -0800296status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300297{
298 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800299 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300300 return res;
301}
302
Dan Stozad3182402014-11-17 12:03:59 -0800303status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
304 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300305{
Craig Donnere96a3252017-02-02 12:13:34 -0800306 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
307}
308
309status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
310 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
311{
Dan Stozad3182402014-11-17 12:03:59 -0800312 if (rect.left < 0 || rect.right > width ||
313 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300314 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
315 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800316 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300317 return BAD_VALUE;
318 }
Craig Donnere96a3252017-02-02 12:13:34 -0800319 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
320 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300321 return res;
322}
323
Dan Stozad3182402014-11-17 12:03:59 -0800324status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
325 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300326{
327 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800328 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300329 return res;
330}
331
Dan Stozad3182402014-11-17 12:03:59 -0800332status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
333 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300334{
Dan Stozad3182402014-11-17 12:03:59 -0800335 if (rect.left < 0 || rect.right > width ||
336 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300337 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
338 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800339 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300340 return BAD_VALUE;
341 }
Dan Stozad3182402014-11-17 12:03:59 -0800342 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
343 ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300344 return res;
345}
346
347status_t GraphicBuffer::unlockAsync(int *fenceFd)
348{
349 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
350 return res;
351}
352
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800353size_t GraphicBuffer::getFlattenedSize() const {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700354 return static_cast<size_t>(12 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800355}
Mathias Agopian3330b202009-10-05 17:07:12 -0700356
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800357size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800358 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800359}
360
Mathias Agopiane1424282013-07-29 21:24:40 -0700361status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800362 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
363 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700364
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800365 size_t fdCountNeeded = GraphicBuffer::getFdCount();
366 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700367
Dan Stozab1363d32014-03-28 15:10:52 -0700368 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800369 buf[0] = 'GBFR';
370 buf[1] = width;
371 buf[2] = height;
372 buf[3] = stride;
373 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700374 buf[5] = static_cast<int32_t>(layerCount);
375 buf[6] = usage;
376 buf[7] = static_cast<int32_t>(mId >> 32);
377 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
378 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700379 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700380 buf[11] = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800381
382 if (handle) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700383 buf[10] = handle->numFds;
384 buf[11] = handle->numInts;
Dan Stozad3182402014-11-17 12:03:59 -0800385 memcpy(fds, handle->data,
386 static_cast<size_t>(handle->numFds) * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700387 memcpy(&buf[12], handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800388 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700389 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800390
Dan Stozaeea6d682015-04-20 12:07:13 -0700391 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700392 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700393 if (handle) {
394 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800395 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700396 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700397
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800398 return NO_ERROR;
399}
400
Mathias Agopiane1424282013-07-29 21:24:40 -0700401status_t GraphicBuffer::unflatten(
402 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700403 if (size < 12 * sizeof(int)) return NO_MEMORY;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800404
405 int const* buf = static_cast<int const*>(buffer);
406 if (buf[0] != 'GBFR') return BAD_TYPE;
407
Craig Donner6ebc46a2016-10-21 15:23:44 -0700408 const size_t numFds = static_cast<size_t>(buf[10]);
409 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800410
Michael Lentinec168b8a2015-02-18 10:14:18 -0800411 // Limit the maxNumber to be relatively small. The number of fds or ints
412 // should not come close to this number, and the number itself was simply
413 // chosen to be high enough to not cause issues and low enough to prevent
414 // overflow problems.
415 const size_t maxNumber = 4096;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700416 if (numFds >= maxNumber || numInts >= (maxNumber - 12)) {
417 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700418 handle = NULL;
Dan Stoza01049c82014-11-11 10:32:31 -0800419 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
Michael Lentine38803262014-10-31 15:25:03 -0700420 numFds, numInts);
421 return BAD_VALUE;
422 }
423
Craig Donner6ebc46a2016-10-21 15:23:44 -0700424 const size_t sizeNeeded = (12 + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800425 if (size < sizeNeeded) return NO_MEMORY;
426
Michael Lentine38803262014-10-31 15:25:03 -0700427 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800428 if (count < fdCountNeeded) return NO_MEMORY;
429
430 if (handle) {
431 // free previous handle if any
432 free_handle();
433 }
434
435 if (numFds || numInts) {
436 width = buf[1];
437 height = buf[2];
438 stride = buf[3];
439 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700440 layerCount = static_cast<uintptr_t>(buf[5]);
441 usage = buf[6];
Dan Stozad3182402014-11-17 12:03:59 -0800442 native_handle* h = native_handle_create(
443 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700444 if (!h) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700445 width = height = stride = format = layerCount = usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700446 handle = NULL;
447 ALOGE("unflatten: native_handle_create failed");
448 return NO_MEMORY;
449 }
Dan Stozad3182402014-11-17 12:03:59 -0800450 memcpy(h->data, fds, numFds * sizeof(int));
Craig Donner6ebc46a2016-10-21 15:23:44 -0700451 memcpy(h->data + numFds, &buf[12], numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800452 handle = h;
453 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700454 width = height = stride = format = layerCount = usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800455 handle = NULL;
456 }
457
Craig Donner6ebc46a2016-10-21 15:23:44 -0700458 mId = static_cast<uint64_t>(buf[7]) << 32;
459 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700460
Craig Donner6ebc46a2016-10-21 15:23:44 -0700461 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700462
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800463 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700464
465 if (handle != 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700466 status_t err = mBufferMapper.registerBuffer(this);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700467 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700468 width = height = stride = format = layerCount = usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800469 handle = NULL;
Jamie Gennisd69097f2012-08-30 13:28:23 -0700470 ALOGE("unflatten: registerBuffer failed: %s (%d)",
471 strerror(-err), err);
472 return err;
473 }
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700474 }
475
Dan Stozaeea6d682015-04-20 12:07:13 -0700476 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700477 size -= sizeNeeded;
478 fds += numFds;
479 count -= numFds;
480
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800481 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700482}
483
Mathias Agopian3330b202009-10-05 17:07:12 -0700484// ---------------------------------------------------------------------------
485
486}; // namespace android