diff options
Diffstat (limited to 'libs/rs/rsAllocation.cpp')
-rw-r--r-- | libs/rs/rsAllocation.cpp | 151 |
1 files changed, 121 insertions, 30 deletions
diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp index 7b92b39..fd85b07 100644 --- a/libs/rs/rsAllocation.cpp +++ b/libs/rs/rsAllocation.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2009-2012 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. @@ -71,11 +71,11 @@ void Allocation::read(void *data) { } void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod, - uint32_t count, const void *data, uint32_t sizeBytes) { - const uint32_t eSize = mHal.state.type->getElementSizeBytes(); + uint32_t count, const void *data, size_t sizeBytes) { + const size_t eSize = mHal.state.type->getElementSizeBytes(); if ((count * eSize) != sizeBytes) { - ALOGE("Allocation::subData called with mismatched size expected %i, got %i", + ALOGE("Allocation::subData called with mismatched size expected %zu, got %zu", (count * eSize), sizeBytes); mHal.state.type->dumpLOGV("type info"); return; @@ -86,14 +86,14 @@ void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod, } void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, - uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) { - const uint32_t eSize = mHal.state.elementSizeBytes; - const uint32_t lineSize = eSize * w; + uint32_t w, uint32_t h, const void *data, size_t sizeBytes) { + const size_t eSize = mHal.state.elementSizeBytes; + const size_t lineSize = eSize * w; //ALOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes); if ((lineSize * h) != sizeBytes) { - ALOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes); + ALOGE("Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes); rsAssert(!"Allocation::subData called with mismatched size"); return; } @@ -104,12 +104,12 @@ void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face, - uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) { + uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes) { } void Allocation::elementData(Context *rsc, uint32_t x, const void *data, - uint32_t cIdx, uint32_t sizeBytes) { - uint32_t eSize = mHal.state.elementSizeBytes; + uint32_t cIdx, size_t sizeBytes) { + size_t eSize = mHal.state.elementSizeBytes; if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { ALOGE("Error Allocation::subElementData component %i out of range.", cIdx); @@ -124,8 +124,9 @@ void Allocation::elementData(Context *rsc, uint32_t x, const void *data, } const Element * e = mHal.state.type->getElement()->getField(cIdx); - if (sizeBytes != e->getSizeBytes()) { - ALOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes()); + uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx); + if (sizeBytes != e->getSizeBytes() * elemArraySize) { + ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes()); rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); return; } @@ -135,8 +136,8 @@ void Allocation::elementData(Context *rsc, uint32_t x, const void *data, } void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, - const void *data, uint32_t cIdx, uint32_t sizeBytes) { - uint32_t eSize = mHal.state.elementSizeBytes; + const void *data, uint32_t cIdx, size_t sizeBytes) { + size_t eSize = mHal.state.elementSizeBytes; if (x >= mHal.state.dimensionX) { ALOGE("Error Allocation::subElementData X offset %i out of range.", x); @@ -157,9 +158,9 @@ void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, } const Element * e = mHal.state.type->getElement()->getField(cIdx); - - if (sizeBytes != e->getSizeBytes()) { - ALOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes()); + uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx); + if (sizeBytes != e->getSizeBytes() * elemArraySize) { + ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes()); rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); return; } @@ -195,6 +196,81 @@ void Allocation::dumpLOGV(const char *prefix) const { prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl); } +uint32_t Allocation::getPackedSize() const { + uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(); + return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded(); +} + +void Allocation::writePackedData(const Type *type, + uint8_t *dst, const uint8_t *src, bool dstPadded) { + const Element *elem = type->getElement(); + uint32_t unpaddedBytes = elem->getSizeBytesUnpadded(); + uint32_t paddedBytes = elem->getSizeBytes(); + uint32_t numItems = type->getSizeBytes() / paddedBytes; + + uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes; + uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes; + + // no sub-elements + uint32_t fieldCount = elem->getFieldCount(); + if (fieldCount == 0) { + for (uint32_t i = 0; i < numItems; i ++) { + memcpy(dst, src, unpaddedBytes); + src += srcInc; + dst += dstInc; + } + return; + } + + // Cache offsets + uint32_t *offsetsPadded = new uint32_t[fieldCount]; + uint32_t *offsetsUnpadded = new uint32_t[fieldCount]; + uint32_t *sizeUnpadded = new uint32_t[fieldCount]; + + for (uint32_t i = 0; i < fieldCount; i++) { + offsetsPadded[i] = elem->getFieldOffsetBytes(i); + offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i); + sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded(); + } + + uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded; + uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded; + + // complex elements, need to copy subelem after subelem + for (uint32_t i = 0; i < numItems; i ++) { + for (uint32_t fI = 0; fI < fieldCount; fI++) { + memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]); + } + src += srcInc; + dst += dstInc; + } + + delete[] offsetsPadded; + delete[] offsetsUnpadded; + delete[] sizeUnpadded; +} + +void Allocation::unpackVec3Allocation(const void *data, size_t dataSize) { + const uint8_t *src = (const uint8_t*)data; + uint8_t *dst = (uint8_t*)getPtr(); + + writePackedData(getType(), dst, src, true); +} + +void Allocation::packVec3Allocation(OStream *stream) const { + uint32_t paddedBytes = getType()->getElement()->getSizeBytes(); + uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded(); + uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes; + + const uint8_t *src = (const uint8_t*)getPtr(); + uint8_t *dst = new uint8_t[numItems * unpaddedBytes]; + + writePackedData(getType(), dst, src, false); + stream->addByteArray(dst, getPackedSize()); + + delete[] dst; +} + void Allocation::serialize(OStream *stream) const { // Need to identify ourselves stream->addU32((uint32_t)getClassId()); @@ -207,10 +283,17 @@ void Allocation::serialize(OStream *stream) const { mHal.state.type->serialize(stream); uint32_t dataSize = mHal.state.type->getSizeBytes(); + // 3 element vectors are padded to 4 in memory, but padding isn't serialized + uint32_t packedSize = getPackedSize(); // Write how much data we are storing - stream->addU32(dataSize); - // Now write the data - stream->addByteArray(getPtr(), dataSize); + stream->addU32(packedSize); + if (dataSize == packedSize) { + // Now write the data + stream->addByteArray(getPtr(), dataSize); + } else { + // Now write the data + packVec3Allocation(stream); + } } Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) { @@ -230,22 +313,30 @@ Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) { } type->compute(); + Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT); + type->decUserRef(); + // Number of bytes we wrote out for this allocation uint32_t dataSize = stream->loadU32(); - if (dataSize != type->getSizeBytes()) { + // 3 element vectors are padded to 4 in memory, but padding isn't serialized + uint32_t packedSize = alloc->getPackedSize(); + if (dataSize != type->getSizeBytes() && + dataSize != packedSize) { ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n"); + ObjectBase::checkDelete(alloc); ObjectBase::checkDelete(type); return NULL; } - Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT); alloc->setName(name.string(), name.size()); - type->decUserRef(); - uint32_t count = dataSize / type->getElementSizeBytes(); - - // Read in all of our allocation data - alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize); + if (dataSize == type->getSizeBytes()) { + uint32_t count = dataSize / type->getElementSizeBytes(); + // Read in all of our allocation data + alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize); + } else { + alloc->unpackVec3Allocation(stream->getPtr() + stream->getPos(), dataSize); + } stream->reset(stream->getPos() + dataSize); return alloc; @@ -429,13 +520,13 @@ void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t } void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face, - const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped + const void *data, size_t sizeBytes, size_t eoff) { Allocation *a = static_cast<Allocation *>(va); a->elementData(rsc, x, y, data, eoff, sizeBytes); } void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod, - const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped + const void *data, size_t sizeBytes, size_t eoff) { Allocation *a = static_cast<Allocation *>(va); a->elementData(rsc, x, data, eoff, sizeBytes); } |