2009-10-06 00:07:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-02-12 01:30:52 +00:00
|
|
|
#define LOG_TAG "GraphicBuffer"
|
|
|
|
|
2009-10-06 00:07:12 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <utils/Errors.h>
|
|
|
|
#include <utils/Log.h>
|
|
|
|
|
|
|
|
#include <ui/GraphicBuffer.h>
|
|
|
|
#include <ui/GraphicBufferAllocator.h>
|
|
|
|
#include <ui/GraphicBufferMapper.h>
|
|
|
|
#include <ui/PixelFormat.h>
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
// ===========================================================================
|
2011-05-01 18:33:26 +00:00
|
|
|
// Buffer and implementation of ANativeWindowBuffer
|
2009-10-06 00:07:12 +00:00
|
|
|
// ===========================================================================
|
|
|
|
|
2014-03-28 22:10:52 +00:00
|
|
|
static uint64_t getUniqueId() {
|
|
|
|
static volatile int32_t nextId = 0;
|
|
|
|
uint64_t id = static_cast<uint64_t>(getpid()) << 32;
|
|
|
|
id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2009-10-06 00:07:12 +00:00
|
|
|
GraphicBuffer::GraphicBuffer()
|
2009-10-27 03:12:37 +00:00
|
|
|
: BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
|
2014-03-28 22:10:52 +00:00
|
|
|
mInitCheck(NO_ERROR), mId(getUniqueId())
|
|
|
|
{
|
2014-11-11 18:32:31 +00:00
|
|
|
width =
|
|
|
|
height =
|
|
|
|
stride =
|
|
|
|
format =
|
2009-10-06 00:07:12 +00:00
|
|
|
usage = 0;
|
|
|
|
handle = NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
|
|
|
|
PixelFormat inFormat, uint32_t inUsage)
|
2009-10-27 03:12:37 +00:00
|
|
|
: BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
|
2014-03-28 22:10:52 +00:00
|
|
|
mInitCheck(NO_ERROR), mId(getUniqueId())
|
2009-10-06 00:07:12 +00:00
|
|
|
{
|
2014-11-11 18:32:31 +00:00
|
|
|
width =
|
|
|
|
height =
|
|
|
|
stride =
|
|
|
|
format =
|
2009-10-06 00:07:12 +00:00
|
|
|
usage = 0;
|
|
|
|
handle = NULL;
|
2014-11-17 20:03:59 +00:00
|
|
|
mInitCheck = initSize(inWidth, inHeight, inFormat, inUsage);
|
2009-10-06 00:07:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
|
|
|
|
PixelFormat inFormat, uint32_t inUsage, uint32_t inStride,
|
|
|
|
native_handle_t* inHandle, bool keepOwnership)
|
2009-10-27 03:12:37 +00:00
|
|
|
: BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
|
|
|
|
mBufferMapper(GraphicBufferMapper::get()),
|
2014-03-28 22:10:52 +00:00
|
|
|
mInitCheck(NO_ERROR), mId(getUniqueId())
|
2009-10-27 03:12:37 +00:00
|
|
|
{
|
2014-11-17 20:03:59 +00:00
|
|
|
width = static_cast<int>(inWidth);
|
|
|
|
height = static_cast<int>(inHeight);
|
|
|
|
stride = static_cast<int>(inStride);
|
2009-10-27 03:12:37 +00:00
|
|
|
format = inFormat;
|
2014-11-17 20:03:59 +00:00
|
|
|
usage = static_cast<int>(inUsage);
|
2009-10-27 03:12:37 +00:00
|
|
|
handle = inHandle;
|
|
|
|
}
|
|
|
|
|
2011-05-01 18:33:26 +00:00
|
|
|
GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership)
|
2010-10-07 20:46:55 +00:00
|
|
|
: BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
|
|
|
|
mBufferMapper(GraphicBufferMapper::get()),
|
2014-03-28 22:10:52 +00:00
|
|
|
mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId())
|
2010-10-07 20:46:55 +00:00
|
|
|
{
|
|
|
|
width = buffer->width;
|
|
|
|
height = buffer->height;
|
|
|
|
stride = buffer->stride;
|
|
|
|
format = buffer->format;
|
|
|
|
usage = buffer->usage;
|
|
|
|
handle = buffer->handle;
|
|
|
|
}
|
|
|
|
|
2010-02-12 01:30:52 +00:00
|
|
|
GraphicBuffer::~GraphicBuffer()
|
2009-10-06 00:07:12 +00:00
|
|
|
{
|
2010-02-12 01:30:52 +00:00
|
|
|
if (handle) {
|
|
|
|
free_handle();
|
2009-10-06 00:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-12 01:30:52 +00:00
|
|
|
void GraphicBuffer::free_handle()
|
2009-10-06 00:07:12 +00:00
|
|
|
{
|
2010-02-12 01:30:52 +00:00
|
|
|
if (mOwner == ownHandle) {
|
2010-10-07 20:46:55 +00:00
|
|
|
mBufferMapper.unregisterBuffer(handle);
|
2010-02-12 01:30:52 +00:00
|
|
|
native_handle_close(handle);
|
|
|
|
native_handle_delete(const_cast<native_handle*>(handle));
|
|
|
|
} else if (mOwner == ownData) {
|
|
|
|
GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
|
|
|
|
allocator.free(handle);
|
2009-10-06 00:07:12 +00:00
|
|
|
}
|
2010-10-07 20:46:55 +00:00
|
|
|
mWrappedBuffer = 0;
|
2009-10-06 00:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t GraphicBuffer::initCheck() const {
|
2014-12-01 23:15:31 +00:00
|
|
|
return static_cast<status_t>(mInitCheck);
|
2009-10-06 00:07:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-04 01:33:09 +00:00
|
|
|
void GraphicBuffer::dumpAllocationsToSystemLog()
|
|
|
|
{
|
|
|
|
GraphicBufferAllocator::dumpToSystemLog();
|
|
|
|
}
|
|
|
|
|
2011-05-01 18:33:26 +00:00
|
|
|
ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
|
2009-10-06 00:07:12 +00:00
|
|
|
{
|
2014-07-22 22:55:08 +00:00
|
|
|
LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
|
2011-05-01 18:33:26 +00:00
|
|
|
return static_cast<ANativeWindowBuffer*>(
|
2009-10-06 00:07:12 +00:00
|
|
|
const_cast<GraphicBuffer*>(this));
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
|
|
|
|
PixelFormat inFormat, uint32_t inUsage)
|
2009-10-06 00:07:12 +00:00
|
|
|
{
|
2009-10-27 03:12:37 +00:00
|
|
|
if (mOwner != ownData)
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
if (handle &&
|
|
|
|
static_cast<int>(inWidth) == width &&
|
|
|
|
static_cast<int>(inHeight) == height &&
|
|
|
|
inFormat == format &&
|
|
|
|
static_cast<int>(inUsage) == usage)
|
2010-06-09 02:54:15 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
|
2009-10-06 00:07:12 +00:00
|
|
|
if (handle) {
|
|
|
|
GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
|
|
|
|
allocator.free(handle);
|
|
|
|
handle = 0;
|
|
|
|
}
|
2014-11-17 20:03:59 +00:00
|
|
|
return initSize(inWidth, inHeight, inFormat, inUsage);
|
2009-10-06 00:07:12 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 00:28:43 +00:00
|
|
|
bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
|
|
|
|
PixelFormat inFormat, uint32_t inUsage)
|
|
|
|
{
|
|
|
|
if (static_cast<int>(inWidth) != width) return true;
|
|
|
|
if (static_cast<int>(inHeight) != height) return true;
|
|
|
|
if (inFormat != format) return true;
|
|
|
|
if ((static_cast<uint32_t>(usage) & inUsage) != inUsage) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
|
|
|
|
PixelFormat inFormat, uint32_t inUsage)
|
2009-10-06 00:07:12 +00:00
|
|
|
{
|
|
|
|
GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
|
2014-11-17 20:03:59 +00:00
|
|
|
uint32_t outStride = 0;
|
|
|
|
status_t err = allocator.alloc(inWidth, inHeight, inFormat, inUsage,
|
|
|
|
&handle, &outStride);
|
2009-10-06 00:07:12 +00:00
|
|
|
if (err == NO_ERROR) {
|
2014-11-17 20:03:59 +00:00
|
|
|
width = static_cast<int>(inWidth);
|
|
|
|
height = static_cast<int>(inHeight);
|
|
|
|
format = inFormat;
|
|
|
|
usage = static_cast<int>(inUsage);
|
|
|
|
stride = static_cast<int>(outStride);
|
2009-10-06 00:07:12 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
|
2009-10-06 00:07:12 +00:00
|
|
|
{
|
|
|
|
const Rect lockBounds(width, height);
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t res = lock(inUsage, lockBounds, vaddr);
|
2009-10-06 00:07:12 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
|
2009-10-06 00:07:12 +00:00
|
|
|
{
|
2014-11-17 20:03:59 +00:00
|
|
|
if (rect.left < 0 || rect.right > width ||
|
|
|
|
rect.top < 0 || rect.bottom > height) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
|
2014-11-11 18:32:31 +00:00
|
|
|
rect.left, rect.top, rect.right, rect.bottom,
|
2014-11-17 20:03:59 +00:00
|
|
|
width, height);
|
2009-10-06 00:07:12 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
|
2009-10-06 00:07:12 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
|
2013-05-05 01:07:43 +00:00
|
|
|
{
|
|
|
|
const Rect lockBounds(width, height);
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
|
2013-05-05 01:07:43 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
|
|
|
|
android_ycbcr* ycbcr)
|
2013-05-05 01:07:43 +00:00
|
|
|
{
|
2014-11-17 20:03:59 +00:00
|
|
|
if (rect.left < 0 || rect.right > width ||
|
|
|
|
rect.top < 0 || rect.bottom > height) {
|
2013-05-05 01:07:43 +00:00
|
|
|
ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
|
|
|
|
rect.left, rect.top, rect.right, rect.bottom,
|
2014-11-17 20:03:59 +00:00
|
|
|
width, height);
|
2013-05-05 01:07:43 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
|
2013-05-05 01:07:43 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-10-06 00:07:12 +00:00
|
|
|
status_t GraphicBuffer::unlock()
|
|
|
|
{
|
|
|
|
status_t res = getBufferMapper().unlock(handle);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
|
2014-04-01 12:30:53 +00:00
|
|
|
{
|
|
|
|
const Rect lockBounds(width, height);
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
|
2014-04-01 12:30:53 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
|
|
|
|
void** vaddr, int fenceFd)
|
2014-04-01 12:30:53 +00:00
|
|
|
{
|
2014-11-17 20:03:59 +00:00
|
|
|
if (rect.left < 0 || rect.right > width ||
|
|
|
|
rect.top < 0 || rect.bottom > height) {
|
2014-04-01 12:30:53 +00:00
|
|
|
ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
|
|
|
|
rect.left, rect.top, rect.right, rect.bottom,
|
2014-11-17 20:03:59 +00:00
|
|
|
width, height);
|
2014-04-01 12:30:53 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t res = getBufferMapper().lockAsync(handle, inUsage, rect, vaddr,
|
|
|
|
fenceFd);
|
2014-04-01 12:30:53 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
|
|
|
|
int fenceFd)
|
2014-04-01 12:30:53 +00:00
|
|
|
{
|
|
|
|
const Rect lockBounds(width, height);
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
|
2014-04-01 12:30:53 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
|
|
|
|
android_ycbcr* ycbcr, int fenceFd)
|
2014-04-01 12:30:53 +00:00
|
|
|
{
|
2014-11-17 20:03:59 +00:00
|
|
|
if (rect.left < 0 || rect.right > width ||
|
|
|
|
rect.top < 0 || rect.bottom > height) {
|
2014-04-01 12:30:53 +00:00
|
|
|
ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
|
|
|
|
rect.left, rect.top, rect.right, rect.bottom,
|
2014-11-17 20:03:59 +00:00
|
|
|
width, height);
|
2014-04-01 12:30:53 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
|
|
|
|
ycbcr, fenceFd);
|
2014-04-01 12:30:53 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t GraphicBuffer::unlockAsync(int *fenceFd)
|
|
|
|
{
|
|
|
|
status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-02-12 01:30:52 +00:00
|
|
|
size_t GraphicBuffer::getFlattenedSize() const {
|
2014-11-17 20:03:59 +00:00
|
|
|
return static_cast<size_t>(10 + (handle ? handle->numInts : 0)) * sizeof(int);
|
2010-02-12 01:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t GraphicBuffer::getFdCount() const {
|
2014-11-17 20:03:59 +00:00
|
|
|
return static_cast<size_t>(handle ? handle->numFds : 0);
|
2010-02-12 01:30:52 +00:00
|
|
|
}
|
2009-10-06 00:07:12 +00:00
|
|
|
|
2013-07-30 04:24:40 +00:00
|
|
|
status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
|
2010-02-12 01:30:52 +00:00
|
|
|
size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
|
|
|
|
if (size < sizeNeeded) return NO_MEMORY;
|
|
|
|
|
|
|
|
size_t fdCountNeeded = GraphicBuffer::getFdCount();
|
|
|
|
if (count < fdCountNeeded) return NO_MEMORY;
|
|
|
|
|
2014-03-28 22:10:52 +00:00
|
|
|
int32_t* buf = static_cast<int32_t*>(buffer);
|
2010-02-12 01:30:52 +00:00
|
|
|
buf[0] = 'GBFR';
|
|
|
|
buf[1] = width;
|
|
|
|
buf[2] = height;
|
|
|
|
buf[3] = stride;
|
|
|
|
buf[4] = format;
|
|
|
|
buf[5] = usage;
|
2014-03-28 22:10:52 +00:00
|
|
|
buf[6] = static_cast<int32_t>(mId >> 32);
|
|
|
|
buf[7] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
|
|
|
|
buf[8] = 0;
|
|
|
|
buf[9] = 0;
|
2009-10-06 00:07:12 +00:00
|
|
|
|
2010-02-12 01:30:52 +00:00
|
|
|
if (handle) {
|
2014-03-28 22:10:52 +00:00
|
|
|
buf[8] = handle->numFds;
|
|
|
|
buf[9] = handle->numInts;
|
2014-11-17 20:03:59 +00:00
|
|
|
memcpy(fds, handle->data,
|
|
|
|
static_cast<size_t>(handle->numFds) * sizeof(int));
|
|
|
|
memcpy(&buf[10], handle->data + handle->numFds,
|
|
|
|
static_cast<size_t>(handle->numInts) * sizeof(int));
|
2010-02-12 01:30:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 19:07:13 +00:00
|
|
|
buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
|
2013-07-30 04:24:40 +00:00
|
|
|
size -= sizeNeeded;
|
2014-03-17 23:48:23 +00:00
|
|
|
if (handle) {
|
|
|
|
fds += handle->numFds;
|
2014-11-17 20:03:59 +00:00
|
|
|
count -= static_cast<size_t>(handle->numFds);
|
2014-03-17 23:48:23 +00:00
|
|
|
}
|
2013-07-30 04:24:40 +00:00
|
|
|
|
2010-02-12 01:30:52 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-07-30 04:24:40 +00:00
|
|
|
status_t GraphicBuffer::unflatten(
|
|
|
|
void const*& buffer, size_t& size, int const*& fds, size_t& count) {
|
2010-02-12 01:30:52 +00:00
|
|
|
if (size < 8*sizeof(int)) return NO_MEMORY;
|
|
|
|
|
|
|
|
int const* buf = static_cast<int const*>(buffer);
|
|
|
|
if (buf[0] != 'GBFR') return BAD_TYPE;
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
const size_t numFds = static_cast<size_t>(buf[8]);
|
|
|
|
const size_t numInts = static_cast<size_t>(buf[9]);
|
2009-10-06 00:07:12 +00:00
|
|
|
|
2015-02-18 18:14:18 +00:00
|
|
|
// Limit the maxNumber to be relatively small. The number of fds or ints
|
|
|
|
// should not come close to this number, and the number itself was simply
|
|
|
|
// chosen to be high enough to not cause issues and low enough to prevent
|
|
|
|
// overflow problems.
|
|
|
|
const size_t maxNumber = 4096;
|
2014-10-31 22:25:03 +00:00
|
|
|
if (numFds >= maxNumber || numInts >= (maxNumber - 10)) {
|
|
|
|
width = height = stride = format = usage = 0;
|
|
|
|
handle = NULL;
|
2014-11-11 18:32:31 +00:00
|
|
|
ALOGE("unflatten: numFds or numInts is too large: %zd, %zd",
|
2014-10-31 22:25:03 +00:00
|
|
|
numFds, numInts);
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2014-03-28 22:10:52 +00:00
|
|
|
const size_t sizeNeeded = (10 + numInts) * sizeof(int);
|
2010-02-12 01:30:52 +00:00
|
|
|
if (size < sizeNeeded) return NO_MEMORY;
|
|
|
|
|
2014-10-31 22:25:03 +00:00
|
|
|
size_t fdCountNeeded = numFds;
|
2010-02-12 01:30:52 +00:00
|
|
|
if (count < fdCountNeeded) return NO_MEMORY;
|
|
|
|
|
|
|
|
if (handle) {
|
|
|
|
// free previous handle if any
|
|
|
|
free_handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numFds || numInts) {
|
|
|
|
width = buf[1];
|
|
|
|
height = buf[2];
|
|
|
|
stride = buf[3];
|
|
|
|
format = buf[4];
|
|
|
|
usage = buf[5];
|
2014-11-17 20:03:59 +00:00
|
|
|
native_handle* h = native_handle_create(
|
|
|
|
static_cast<int>(numFds), static_cast<int>(numInts));
|
2014-10-31 22:25:03 +00:00
|
|
|
if (!h) {
|
|
|
|
width = height = stride = format = usage = 0;
|
|
|
|
handle = NULL;
|
|
|
|
ALOGE("unflatten: native_handle_create failed");
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
2014-11-17 20:03:59 +00:00
|
|
|
memcpy(h->data, fds, numFds * sizeof(int));
|
|
|
|
memcpy(h->data + numFds, &buf[10], numInts * sizeof(int));
|
2010-02-12 01:30:52 +00:00
|
|
|
handle = h;
|
2009-10-06 00:07:12 +00:00
|
|
|
} else {
|
2010-02-12 01:30:52 +00:00
|
|
|
width = height = stride = format = usage = 0;
|
|
|
|
handle = NULL;
|
2009-10-06 00:07:12 +00:00
|
|
|
}
|
2010-02-12 01:30:52 +00:00
|
|
|
|
2014-03-28 22:10:52 +00:00
|
|
|
mId = static_cast<uint64_t>(buf[6]) << 32;
|
|
|
|
mId |= static_cast<uint32_t>(buf[7]);
|
|
|
|
|
2010-02-12 01:30:52 +00:00
|
|
|
mOwner = ownHandle;
|
2010-10-07 20:46:55 +00:00
|
|
|
|
|
|
|
if (handle != 0) {
|
2012-08-30 20:28:23 +00:00
|
|
|
status_t err = mBufferMapper.registerBuffer(handle);
|
|
|
|
if (err != NO_ERROR) {
|
2012-11-20 11:24:35 +00:00
|
|
|
width = height = stride = format = usage = 0;
|
|
|
|
handle = NULL;
|
2012-08-30 20:28:23 +00:00
|
|
|
ALOGE("unflatten: registerBuffer failed: %s (%d)",
|
|
|
|
strerror(-err), err);
|
|
|
|
return err;
|
|
|
|
}
|
2010-10-07 20:46:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 19:07:13 +00:00
|
|
|
buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
|
2013-07-30 04:24:40 +00:00
|
|
|
size -= sizeNeeded;
|
|
|
|
fds += numFds;
|
|
|
|
count -= numFds;
|
|
|
|
|
2010-02-12 01:30:52 +00:00
|
|
|
return NO_ERROR;
|
2009-10-06 00:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
}; // namespace android
|