diff options
Diffstat (limited to 'libs/rs/rsAllocation.cpp')
-rw-r--r-- | libs/rs/rsAllocation.cpp | 133 |
1 files changed, 129 insertions, 4 deletions
diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp index 4e8278d..7e44fea 100644 --- a/libs/rs/rsAllocation.cpp +++ b/libs/rs/rsAllocation.cpp @@ -13,12 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#ifndef ANDROID_RS_BUILD_FOR_HOST #include "rsContext.h" #include <GLES/gl.h> #include <GLES2/gl2.h> #include <GLES/glext.h> +#else +#include "rsContextHostStub.h" + +#include <OpenGL/gl.h> +#include <OpenGl/glext.h> +#endif using namespace android; using namespace android::renderscript; @@ -28,6 +34,9 @@ Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc) init(rsc, type); mPtr = malloc(mType->getSizeBytes()); + if (mType->getElement()->getHasReferences()) { + memset(mPtr, 0, mType->getSizeBytes()); + } if (!mPtr) { LOGE("Allocation::Allocation, alloc failure"); } @@ -167,9 +176,12 @@ void Allocation::uploadToTexture(const Context *rsc) 0, format, type, ptr); } if (mTextureGenMipmap) { +#ifndef ANDROID_RS_BUILD_FOR_HOST glGenerateMipmap(GL_TEXTURE_2D); +#endif //ANDROID_RS_BUILD_FOR_HOST } + rsc->checkError("Allocation::uploadToTexture"); } void Allocation::deferedUploadToBufferObject(const Context *rsc) @@ -201,6 +213,7 @@ void Allocation::uploadToBufferObject(const Context *rsc) glBindBuffer(GL_ARRAY_BUFFER, mBufferID); glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); + rsc->checkError("Allocation::uploadToBufferObject"); } void Allocation::uploadCheck(const Context *rsc) @@ -224,6 +237,12 @@ void Allocation::data(const void *data, uint32_t sizeBytes) LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes); return; } + + if (mType->getElement()->getHasReferences()) { + incRefs(data, sizeBytes / mType->getElement()->getSizeBytes()); + decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes()); + } + memcpy(mPtr, data, size); sendDirty(); mUploadDefered = true; @@ -246,6 +265,12 @@ void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32 mType->dumpLOGV("type info"); return; } + + if (mType->getElement()->getHasReferences()) { + incRefs(data, count); + decRefs(ptr, count); + } + memcpy(ptr, data, size); sendDirty(); mUploadDefered = true; @@ -269,6 +294,10 @@ void Allocation::subData(uint32_t xoff, uint32_t yoff, for (uint32_t line=yoff; line < (yoff+h); line++) { uint8_t * ptr = static_cast<uint8_t *>(mPtr); + if (mType->getElement()->getHasReferences()) { + incRefs(src, w); + decRefs(dst, w); + } memcpy(dst, src, lineSize); src += lineSize; dst += destW * eSize; @@ -284,7 +313,7 @@ void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff, void Allocation::addProgramToDirty(const Program *p) { - mToDirtyList.add(p); + mToDirtyList.push(p); } void Allocation::removeProgramToDirty(const Program *p) @@ -316,6 +345,61 @@ void Allocation::dumpLOGV(const char *prefix) const } +void Allocation::serialize(OStream *stream) const +{ + // Need to identify ourselves + stream->addU32((uint32_t)getClassId()); + + String8 name(getName()); + stream->addString(&name); + + // First thing we need to serialize is the type object since it will be needed + // to initialize the class + mType->serialize(stream); + + uint32_t dataSize = mType->getSizeBytes(); + // Write how much data we are storing + stream->addU32(dataSize); + // Now write the data + stream->addByteArray(mPtr, dataSize); +} + +Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) +{ + // First make sure we are reading the correct object + RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); + if(classID != RS_A3D_CLASS_ID_ALLOCATION) { + LOGE("allocation loading skipped due to invalid class id\n"); + return NULL; + } + + String8 name; + stream->loadString(&name); + + Type *type = Type::createFromStream(rsc, stream); + if(!type) { + return NULL; + } + type->compute(); + + // Number of bytes we wrote out for this allocation + uint32_t dataSize = stream->loadU32(); + if(dataSize != type->getSizeBytes()) { + LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n"); + delete type; + return NULL; + } + + Allocation *alloc = new Allocation(rsc, type); + alloc->setName(name.string(), name.size()); + + // Read in all of our allocation data + alloc->data(stream->getPtr() + stream->getPos(), dataSize); + stream->reset(stream->getPos() + dataSize); + + return alloc; +} + void Allocation::sendDirty() const { for (size_t ct=0; ct < mToDirtyList.size(); ct++) { @@ -323,6 +407,32 @@ void Allocation::sendDirty() const } } +void Allocation::incRefs(const void *ptr, size_t ct) const +{ + const uint8_t *p = static_cast<const uint8_t *>(ptr); + const Element *e = mType->getElement(); + uint32_t stride = e->getSizeBytes(); + + while (ct > 0) { + e->incRefs(p); + ct --; + p += stride; + } +} + +void Allocation::decRefs(const void *ptr, size_t ct) const +{ + const uint8_t *p = static_cast<const uint8_t *>(ptr); + const Element *e = mType->getElement(); + uint32_t stride = e->getSizeBytes(); + + while (ct > 0) { + e->decRefs(p); + ct --; + p += stride; + } +} + ///////////////// // @@ -495,7 +605,7 @@ static ElementConverter_t pickConverter(const Element *dst, const Element *src) if (srcGLType == GL_UNSIGNED_BYTE && srcGLFmt == GL_RGB && dstGLType == GL_UNSIGNED_SHORT_5_6_5 && - dstGLType == GL_RGB) { + dstGLFmt == GL_RGB) { return elementConverter_888_to_565; } @@ -503,15 +613,21 @@ static ElementConverter_t pickConverter(const Element *dst, const Element *src) if (srcGLType == GL_UNSIGNED_BYTE && srcGLFmt == GL_RGBA && dstGLType == GL_UNSIGNED_SHORT_5_6_5 && - dstGLType == GL_RGB) { + dstGLFmt == GL_RGB) { return elementConverter_8888_to_565; } LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst); + LOGE("pickConverter, srcGLType = %x, srcGLFmt = %x", srcGLType, srcGLFmt); + LOGE("pickConverter, dstGLType = %x, dstGLFmt = %x", dstGLType, dstGLFmt); + src->dumpLOGV("SRC "); + dst->dumpLOGV("DST "); return 0; } +#ifndef ANDROID_RS_BUILD_FOR_HOST + RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype, void *bmp, void *callbackData, RsBitmapCallback_t callback) { @@ -613,6 +729,15 @@ void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) a->read(data); } +const void* rsi_AllocationGetType(Context *rsc, RsAllocation va) +{ + Allocation *a = static_cast<Allocation *>(va); + a->getType()->incUserRef(); + + return a->getType(); +} + +#endif //ANDROID_RS_BUILD_FOR_HOST } } |