libui/libgui: Fix errors in parceling

BufferItem and GraphicBuffer were not parceling correctly, which had
not been noticed because the libgui tests (specifically, one that
tests placing a BufferQueue in a separate process from the IGBP/C)
had not been run recently. This change fixes the errors found in
those classes.

Change-Id: Ie224361a534a79115a3481d83ff97f21d154d4f5
This commit is contained in:
Dan Stoza 2015-04-20 12:07:13 -07:00
parent ff5c1a6b67
commit eea6d682b8
2 changed files with 83 additions and 54 deletions

View File

@ -39,52 +39,66 @@ BufferItem::BufferItem() :
BufferItem::~BufferItem() {} BufferItem::~BufferItem() {}
template <typename T>
static void addAligned(size_t& size, T /* value */) {
size = FlattenableUtils::align<sizeof(T)>(size);
size += sizeof(T);
}
size_t BufferItem::getPodSize() const { size_t BufferItem::getPodSize() const {
size_t c = sizeof(mCrop) + // Must align<8> before writing these fields for this to be correct
sizeof(mTransform) + size_t size = 0;
sizeof(mScalingMode) + addAligned(size, mCrop);
sizeof(mTimestamp) + addAligned(size, mTransform);
sizeof(mIsAutoTimestamp) + addAligned(size, mScalingMode);
sizeof(mDataSpace) + addAligned(size, mTimestamp);
sizeof(mFrameNumber) + addAligned(size, mIsAutoTimestamp);
sizeof(mSlot) + addAligned(size, mDataSpace);
sizeof(mIsDroppable) + addAligned(size, mFrameNumber);
sizeof(mAcquireCalled) + addAligned(size, mSlot);
sizeof(mTransformToDisplayInverse); addAligned(size, mIsDroppable);
return c; addAligned(size, mAcquireCalled);
addAligned(size, mTransformToDisplayInverse);
return size;
} }
size_t BufferItem::getFlattenedSize() const { size_t BufferItem::getFlattenedSize() const {
size_t c = 0; size_t size = sizeof(uint32_t); // Flags
if (mGraphicBuffer != 0) { if (mGraphicBuffer != 0) {
c += mGraphicBuffer->getFlattenedSize(); size += mGraphicBuffer->getFlattenedSize();
FlattenableUtils::align<4>(c); FlattenableUtils::align<4>(size);
} }
if (mFence != 0) { if (mFence != 0) {
c += mFence->getFlattenedSize(); size += mFence->getFlattenedSize();
FlattenableUtils::align<4>(c); FlattenableUtils::align<4>(size);
} }
c += mSurfaceDamage.getFlattenedSize(); size += mSurfaceDamage.getFlattenedSize();
FlattenableUtils::align<4>(c); size = FlattenableUtils::align<8>(size);
return sizeof(int32_t) + c + getPodSize(); return size + getPodSize();
} }
size_t BufferItem::getFdCount() const { size_t BufferItem::getFdCount() const {
size_t c = 0; size_t count = 0;
if (mGraphicBuffer != 0) { if (mGraphicBuffer != 0) {
c += mGraphicBuffer->getFdCount(); count += mGraphicBuffer->getFdCount();
} }
if (mFence != 0) { if (mFence != 0) {
c += mFence->getFdCount(); count += mFence->getFdCount();
} }
return c; return count;
}
template <typename T>
static void writeAligned(void*& buffer, size_t& size, T value) {
size -= FlattenableUtils::align<alignof(T)>(buffer);
FlattenableUtils::write(buffer, size, value);
} }
status_t BufferItem::flatten( status_t BufferItem::flatten(
void*& buffer, size_t& size, int*& fds, size_t& count) const { void*& buffer, size_t& size, int*& fds, size_t& count) const {
// make sure we have enough space // make sure we have enough space
if (count < BufferItem::getFlattenedSize()) { if (size < BufferItem::getFlattenedSize()) {
return NO_MEMORY; return NO_MEMORY;
} }
@ -107,35 +121,46 @@ status_t BufferItem::flatten(
size -= FlattenableUtils::align<4>(buffer); size -= FlattenableUtils::align<4>(buffer);
flags |= 2; flags |= 2;
} }
status_t err = mSurfaceDamage.flatten(buffer, size); status_t err = mSurfaceDamage.flatten(buffer, size);
if (err) return err; if (err) return err;
size -= FlattenableUtils::align<4>(buffer); FlattenableUtils::advance(buffer, size, mSurfaceDamage.getFlattenedSize());
// check we have enough space (in case flattening the fence/graphicbuffer lied to us) // Must align<8> so that getPodSize returns the correct value
size -= FlattenableUtils::align<8>(buffer);
// Check we still have enough space
if (size < getPodSize()) { if (size < getPodSize()) {
return NO_MEMORY; return NO_MEMORY;
} }
FlattenableUtils::write(buffer, size, mCrop); writeAligned(buffer, size, mCrop);
FlattenableUtils::write(buffer, size, mTransform); writeAligned(buffer, size, mTransform);
FlattenableUtils::write(buffer, size, mScalingMode); writeAligned(buffer, size, mScalingMode);
FlattenableUtils::write(buffer, size, mTimestamp); writeAligned(buffer, size, mTimestamp);
FlattenableUtils::write(buffer, size, mIsAutoTimestamp); writeAligned(buffer, size, mIsAutoTimestamp);
FlattenableUtils::write(buffer, size, mDataSpace); writeAligned(buffer, size, mDataSpace);
FlattenableUtils::write(buffer, size, mFrameNumber); writeAligned(buffer, size, mFrameNumber);
FlattenableUtils::write(buffer, size, mSlot); writeAligned(buffer, size, mSlot);
FlattenableUtils::write(buffer, size, mIsDroppable); writeAligned(buffer, size, mIsDroppable);
FlattenableUtils::write(buffer, size, mAcquireCalled); writeAligned(buffer, size, mAcquireCalled);
FlattenableUtils::write(buffer, size, mTransformToDisplayInverse); writeAligned(buffer, size, mTransformToDisplayInverse);
return NO_ERROR; return NO_ERROR;
} }
template <typename T>
static void readAligned(const void*& buffer, size_t& size, T& value) {
size -= FlattenableUtils::align<alignof(T)>(buffer);
FlattenableUtils::read(buffer, size, value);
}
status_t BufferItem::unflatten( status_t BufferItem::unflatten(
void const*& buffer, size_t& size, int const*& fds, size_t& count) { void const*& buffer, size_t& size, int const*& fds, size_t& count) {
if (size < sizeof(uint32_t)) if (size < sizeof(uint32_t)) {
return NO_MEMORY; return NO_MEMORY;
}
uint32_t flags = 0; uint32_t flags = 0;
FlattenableUtils::read(buffer, size, flags); FlattenableUtils::read(buffer, size, flags);
@ -153,26 +178,30 @@ status_t BufferItem::unflatten(
if (err) return err; if (err) return err;
size -= FlattenableUtils::align<4>(buffer); size -= FlattenableUtils::align<4>(buffer);
} }
status_t err = mSurfaceDamage.unflatten(buffer, size); status_t err = mSurfaceDamage.unflatten(buffer, size);
if (err) return err; if (err) return err;
size -= FlattenableUtils::align<4>(buffer); FlattenableUtils::advance(buffer, size, mSurfaceDamage.getFlattenedSize());
// check we have enough space // Must align<8> so that getPodSize returns the correct value
size -= FlattenableUtils::align<8>(buffer);
// Check we still have enough space
if (size < getPodSize()) { if (size < getPodSize()) {
return NO_MEMORY; return NO_MEMORY;
} }
FlattenableUtils::read(buffer, size, mCrop); readAligned(buffer, size, mCrop);
FlattenableUtils::read(buffer, size, mTransform); readAligned(buffer, size, mTransform);
FlattenableUtils::read(buffer, size, mScalingMode); readAligned(buffer, size, mScalingMode);
FlattenableUtils::read(buffer, size, mTimestamp); readAligned(buffer, size, mTimestamp);
FlattenableUtils::read(buffer, size, mIsAutoTimestamp); readAligned(buffer, size, mIsAutoTimestamp);
FlattenableUtils::read(buffer, size, mDataSpace); readAligned(buffer, size, mDataSpace);
FlattenableUtils::read(buffer, size, mFrameNumber); readAligned(buffer, size, mFrameNumber);
FlattenableUtils::read(buffer, size, mSlot); readAligned(buffer, size, mSlot);
FlattenableUtils::read(buffer, size, mIsDroppable); readAligned(buffer, size, mIsDroppable);
FlattenableUtils::read(buffer, size, mAcquireCalled); readAligned(buffer, size, mAcquireCalled);
FlattenableUtils::read(buffer, size, mTransformToDisplayInverse); readAligned(buffer, size, mTransformToDisplayInverse);
return NO_ERROR; return NO_ERROR;
} }

View File

@ -303,7 +303,7 @@ status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t&
static_cast<size_t>(handle->numInts) * sizeof(int)); static_cast<size_t>(handle->numInts) * sizeof(int));
} }
buffer = reinterpret_cast<void*>(static_cast<int*>(buffer) + sizeNeeded); buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
size -= sizeNeeded; size -= sizeNeeded;
if (handle) { if (handle) {
fds += handle->numFds; fds += handle->numFds;
@ -385,7 +385,7 @@ status_t GraphicBuffer::unflatten(
} }
} }
buffer = reinterpret_cast<void const*>(static_cast<int const*>(buffer) + sizeNeeded); buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
size -= sizeNeeded; size -= sizeNeeded;
fds += numFds; fds += numFds;
count -= numFds; count -= numFds;