Cleanup pointer access in adapter and font.

Change-Id: Ie500574adebb9bdb38c138f78582af2cd7610f76
diff --git a/driver/rsdAllocation.cpp b/driver/rsdAllocation.cpp
index 7f2de1d..a180348 100644
--- a/driver/rsdAllocation.cpp
+++ b/driver/rsdAllocation.cpp
@@ -161,7 +161,7 @@
         return;
     }
 
-    if (!alloc->getPtr()) {
+    if (!drv->lod[0].mallocPtr) {
         return;
     }
 
@@ -388,8 +388,8 @@
     drv->readBackFBO->setActive(rsc);
 
     // Do the readback
-    RSD_CALL_GL(glReadPixels, 0, 0, alloc->getType()->getDimX(), alloc->getType()->getDimY(),
-                 drv->glFormat, drv->glType, alloc->getPtr());
+    RSD_CALL_GL(glReadPixels, 0, 0, drv->lod[0].dimX, drv->lod[0].dimY,
+                drv->glFormat, drv->glType, drv->lod[0].mallocPtr);
 
     // Revert framebuffer to its original
     lastFbo->setActive(rsc);
@@ -743,4 +743,84 @@
     drv->uploadDeferred = true;
 }
 
+static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
+    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
+    uint32_t w = drv->lod[lod + 1].dimX;
+    uint32_t h = drv->lod[lod + 1].dimY;
+
+    for (uint32_t y=0; y < h; y++) {
+        uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
+        const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
+        const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
+
+        for (uint32_t x=0; x < w; x++) {
+            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
+            oPtr ++;
+            i1 += 2;
+            i2 += 2;
+        }
+    }
+}
+
+static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
+    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
+    uint32_t w = drv->lod[lod + 1].dimX;
+    uint32_t h = drv->lod[lod + 1].dimY;
+
+    for (uint32_t y=0; y < h; y++) {
+        uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
+        const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
+        const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
+
+        for (uint32_t x=0; x < w; x++) {
+            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
+            oPtr ++;
+            i1 += 2;
+            i2 += 2;
+        }
+    }
+}
+
+static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
+    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
+    uint32_t w = drv->lod[lod + 1].dimX;
+    uint32_t h = drv->lod[lod + 1].dimY;
+
+    for (uint32_t y=0; y < h; y++) {
+        uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, lod + 1, face);
+        const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, lod, face);
+        const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, lod, face);
+
+        for (uint32_t x=0; x < w; x++) {
+            *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
+            oPtr ++;
+            i1 += 2;
+            i2 += 2;
+        }
+    }
+}
+
+void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) {
+    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
+    if(!drv->lod[0].mallocPtr) {
+        return;
+    }
+    uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1;
+    for (uint32_t face = 0; face < numFaces; face ++) {
+        for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) {
+            switch (alloc->getType()->getElement()->getSizeBits()) {
+            case 32:
+                mip8888(alloc, lod, (RsAllocationCubemapFace)face);
+                break;
+            case 16:
+                mip565(alloc, lod, (RsAllocationCubemapFace)face);
+                break;
+            case 8:
+                mip8(alloc, lod, (RsAllocationCubemapFace)face);
+                break;
+            }
+        }
+    }
+}
+
 
diff --git a/driver/rsdAllocation.h b/driver/rsdAllocation.h
index 98b1d65..e6488b9 100644
--- a/driver/rsdAllocation.h
+++ b/driver/rsdAllocation.h
@@ -157,6 +157,8 @@
                                 uint32_t x, uint32_t y,
                                 const void *data, uint32_t elementOff, uint32_t sizeBytes);
 
+void rsdAllocationGenerateMipmaps(const android::renderscript::Context *rsc,
+                                  const android::renderscript::Allocation *alloc);
 
 
 
diff --git a/driver/rsdBcc.cpp b/driver/rsdBcc.cpp
index 00d9cb1..b1cdea6 100644
--- a/driver/rsdBcc.cpp
+++ b/driver/rsdBcc.cpp
@@ -344,7 +344,7 @@
     mtls.eStrideIn = 0;
     if (ain) {
         DrvAllocation *aindrv = (DrvAllocation *)ain->mHal.drv;
-        mtls.ptrIn = (const uint8_t *)ain->getPtr();
+        mtls.ptrIn = (const uint8_t *)aindrv->lod[0].mallocPtr;
         mtls.eStrideIn = ain->getType()->getElementSizeBytes();
         mtls.yStrideIn = aindrv->lod[0].stride;
     }
@@ -353,7 +353,7 @@
     mtls.eStrideOut = 0;
     if (aout) {
         DrvAllocation *aoutdrv = (DrvAllocation *)aout->mHal.drv;
-        mtls.ptrOut = (uint8_t *)aout->getPtr();
+        mtls.ptrOut = (uint8_t *)aoutdrv->lod[0].mallocPtr;
         mtls.eStrideOut = aout->getType()->getElementSizeBytes();
         mtls.yStrideOut = aoutdrv->lod[0].stride;
     }
diff --git a/driver/rsdCore.cpp b/driver/rsdCore.cpp
index 1a71d54..a730acc 100644
--- a/driver/rsdCore.cpp
+++ b/driver/rsdCore.cpp
@@ -88,7 +88,8 @@
         rsdAllocationData2D_alloc,
         rsdAllocationData3D_alloc,
         rsdAllocationElementData1D,
-        rsdAllocationElementData2D
+        rsdAllocationElementData2D,
+        rsdAllocationGenerateMipmaps
     },
 
 
diff --git a/driver/rsdPath.cpp b/driver/rsdPath.cpp
index e04bc02..79ec487 100644
--- a/driver/rsdPath.cpp
+++ b/driver/rsdPath.cpp
@@ -112,7 +112,7 @@
 DrvPathStatic::DrvPathStatic(const Allocation *vtx, const Allocation *loops) {
     mSegmentCount = vtx->getType()->getDimX() / 3;
     mSegments = new segment_t[mSegmentCount];
-
+/*
     const float *fin = (const float *)vtx->getPtr();
     for (uint32_t ct=0; ct < mSegmentCount; ct++) {
         segment_t *s = &mSegments[ct];
@@ -126,6 +126,7 @@
         s->y2 = fin[5];
         fin += 6;
     }
+    */
 }
 
 DrvPathStatic::~DrvPathStatic() {
diff --git a/driver/rsdShader.cpp b/driver/rsdShader.cpp
index d39bdb8..3654090 100644
--- a/driver/rsdShader.cpp
+++ b/driver/rsdShader.cpp
@@ -516,6 +516,7 @@
     uint32_t uidx = 0;
     for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) {
         Allocation *alloc = mRSProgram->mHal.state.constants[ct];
+
         if (!alloc) {
             ALOGE("Attempting to set constants on shader id %u, but alloc at slot %u is not set",
                  (uint32_t)this, ct);
@@ -523,7 +524,8 @@
             continue;
         }
 
-        const uint8_t *data = static_cast<const uint8_t *>(alloc->getPtr());
+        DrvAllocation *adrv = (DrvAllocation *)alloc->mHal.drv;
+        const uint8_t *data = static_cast<const uint8_t *>(adrv->lod[0].mallocPtr);
         const Element *e = mRSProgram->mHal.state.constantTypes[ct]->getElement();
         for (uint32_t field=0; field < e->mHal.state.fieldsCount; field++) {
             const Element *f = e->mHal.state.fields[field];
diff --git a/rsAdapter.cpp b/rsAdapter.cpp
index 807da08..13a728f 100644
--- a/rsAdapter.cpp
+++ b/rsAdapter.cpp
@@ -34,30 +34,11 @@
     mY = 0;
     mZ = 0;
     mLOD = 0;
-    mFace = 0;
+    mFace = RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X;
 }
 
-void * Adapter1D::getElement(uint32_t x) {
-    rsAssert(mAllocation.get());
-    rsAssert(mAllocation->getPtr());
-    rsAssert(mAllocation->getType());
-    uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr());
-    ptr += mAllocation->getType()->getLODOffset(mLOD, x, mY);
-    return ptr;
-}
-
-void Adapter1D::subData(uint32_t xoff, uint32_t count, const void *data) {
-    if (mAllocation.get() && mAllocation.get()->getType()) {
-        void *ptr = getElement(xoff);
-        count *= mAllocation.get()->getType()->getElementSizeBytes();
-        memcpy(ptr, data, count);
-    }
-}
-
-void Adapter1D::data(const void *data) {
-    memcpy(getElement(0),
-           data,
-           mAllocation.get()->getType()->getSizeBytes());
+void Adapter1D::data(Context *rsc, uint32_t x, uint32_t count, const void *data, size_t sizeBytes) {
+    mAllocation->data(rsc, x, mY, mLOD, mFace, count, 1, data, sizeBytes);
 }
 
 void Adapter1D::serialize(Context *rsc, OStream *stream) const {
@@ -98,7 +79,7 @@
         a->setLOD(value);
         break;
     case RS_DIMENSION_FACE:
-        a->setFace(value);
+        a->setFace((RsAllocationCubemapFace)value);
         break;
     default:
         rsAssert(!"Unimplemented constraint");
@@ -106,16 +87,6 @@
     }
 }
 
-void rsi_Adapter1DSubData(Context *rsc, RsAdapter1D va, uint32_t xoff, uint32_t count, const void *data) {
-    Adapter1D * a = static_cast<Adapter1D *>(va);
-    a->subData(xoff, count, data);
-}
-
-void rsi_Adapter1DData(Context *rsc, RsAdapter1D va, const void *data) {
-    Adapter1D * a = static_cast<Adapter1D *>(va);
-    a->data(data);
-}
-
 }
 }
 
@@ -133,49 +104,15 @@
 void Adapter2D::reset() {
     mZ = 0;
     mLOD = 0;
-    mFace = 0;
+    mFace = RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X;
 }
 
-void * Adapter2D::getElement(uint32_t x, uint32_t y) const {
-    rsAssert(mAllocation.get());
-    rsAssert(mAllocation->getPtr());
-    rsAssert(mAllocation->getType());
-    if (mFace != 0 && !mAllocation->getType()->getDimFaces()) {
-        ALOGE("Adapter wants cubemap face, but allocation has none");
-        return NULL;
-    }
 
-    uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr());
-    ptr += mAllocation->getType()->getLODOffset(mLOD, x, y);
-
-    if (mFace != 0) {
-        uint32_t totalSizeBytes = mAllocation->getType()->getSizeBytes();
-        uint32_t faceOffset = totalSizeBytes / 6;
-        ptr += faceOffset * mFace;
-    }
-    return ptr;
+void Adapter2D::data(Context *rsc, uint32_t x, uint32_t y, uint32_t w, uint32_t h,
+                     const void *data, size_t sizeBytes) {
+    mAllocation->data(rsc, x, y, mLOD, mFace, w, h, data, sizeBytes);
 }
 
-void Adapter2D::subData(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) {
-    rsAssert(mAllocation.get());
-    rsAssert(mAllocation->getPtr());
-    rsAssert(mAllocation->getType());
-
-    uint32_t eSize = mAllocation.get()->getType()->getElementSizeBytes();
-    uint32_t lineSize = eSize * w;
-
-    const uint8_t *src = static_cast<const uint8_t *>(data);
-    for (uint32_t line=yoff; line < (yoff+h); line++) {
-        memcpy(getElement(xoff, line), src, lineSize);
-        src += lineSize;
-    }
-}
-
-void Adapter2D::data(const void *data) {
-    memcpy(getElement(0,0),
-           data,
-           mAllocation.get()->getType()->getSizeBytes());
-}
 
 void Adapter2D::serialize(Context *rsc, OStream *stream) const {
 }
@@ -216,7 +153,7 @@
         a->setLOD(value);
         break;
     case RS_DIMENSION_FACE:
-        a->setFace(value);
+        a->setFace((RsAllocationCubemapFace)value);
         break;
     default:
         rsAssert(!"Unimplemented constraint");
@@ -224,15 +161,6 @@
     }
 }
 
-void rsi_Adapter2DData(Context *rsc, RsAdapter2D va, const void *data) {
-    Adapter2D * a = static_cast<Adapter2D *>(va);
-    a->data(data);
-}
-
-void rsi_Adapter2DSubData(Context *rsc, RsAdapter2D va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) {
-    Adapter2D * a = static_cast<Adapter2D *>(va);
-    a->subData(xoff, yoff, w, h, data);
-}
 
 }
 }
diff --git a/rsAdapter.h b/rsAdapter.h
index 4134a98..7b189cf 100644
--- a/rsAdapter.h
+++ b/rsAdapter.h
@@ -32,7 +32,6 @@
     Adapter1D(Context *);
     Adapter1D(Context *, Allocation *);
     void reset();
-    void * getElement(uint32_t x);
 
     void setAllocation(Allocation *a) {mAllocation.set(a);}
 
@@ -43,11 +42,9 @@
     inline void setY(uint32_t y) {mY = y;}
     inline void setZ(uint32_t z) {mZ = z;}
     inline void setLOD(uint32_t lod) {mLOD = lod;}
-    inline void setFace(uint32_t face) {mFace = face;}
-    //void setArray(uint32_t num, uint32_t value);
+    inline void setFace(RsAllocationCubemapFace face) {mFace = face;}
 
-    void subData(uint32_t xoff, uint32_t count, const void *data);
-    void data(const void *data);
+    void data(Context *rsc, uint32_t xoff, uint32_t count, const void *data, size_t sizeBytes);
 
     virtual void serialize(Context *rsc, OStream *stream) const;
     virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ADAPTER_1D; }
@@ -58,7 +55,7 @@
     uint32_t mY;
     uint32_t mZ;
     uint32_t mLOD;
-    uint32_t mFace;
+    RsAllocationCubemapFace mFace;
 };
 
 class Adapter2D : public ObjectBase {
@@ -69,7 +66,6 @@
     Adapter2D(Context *);
     Adapter2D(Context *, Allocation *);
     void reset();
-    void * getElement(uint32_t x, uint32_t y) const;
 
     uint32_t getDimX() const {return mAllocation->getType()->getLODDimX(mLOD);}
     uint32_t getDimY() const {return mAllocation->getType()->getLODDimY(mLOD);}
@@ -78,11 +74,10 @@
     void setAllocation(Allocation *a) {mAllocation.set(a);}
     inline void setZ(uint32_t z) {mZ = z;}
     inline void setLOD(uint32_t lod) {mLOD = lod;}
-    inline void setFace(uint32_t face) {mFace = face;}
-    //void setArray(uint32_t num, uint32_t value);
+    inline void setFace(RsAllocationCubemapFace face) {mFace = face;}
 
-    void data(const void *data);
-    void subData(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data);
+    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
+              const void *data, size_t sizeBytes);
 
     virtual void serialize(Context *rsc, OStream *stream) const;
     virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ADAPTER_2D; }
@@ -92,7 +87,7 @@
     ObjectBaseRef<Allocation> mAllocation;
     uint32_t mZ;
     uint32_t mLOD;
-    uint32_t mFace;
+    RsAllocationCubemapFace mFace;
 };
 
 }
diff --git a/rsAllocation.cpp b/rsAllocation.cpp
index ae1bd91..8c6fedc 100644
--- a/rsAllocation.cpp
+++ b/rsAllocation.cpp
@@ -228,7 +228,7 @@
     }
 
     ALOGV("%s allocation ptr=%p  mUsageFlags=0x04%x, mMipmapControl=0x%04x",
-         prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
+         prefix, mHal.drvState.mallocPtrLOD0, mHal.state.usageFlags, mHal.state.mipmapControl);
 }
 
 uint32_t Allocation::getPackedSize() const {
@@ -287,9 +287,10 @@
 
 void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) {
     const uint8_t *src = (const uint8_t*)data;
-    uint8_t *dst = (uint8_t*)getPtr();
+    uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
 
     writePackedData(rsc, getType(), dst, src, true);
+    rsc->mHal.funcs.allocation.unlock1D(rsc, this);
 }
 
 void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
@@ -297,13 +298,14 @@
     uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
     uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
 
-    const uint8_t *src = (const uint8_t*)getPtr();
+    const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
     uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
 
     writePackedData(rsc, getType(), dst, src, false);
     stream->addByteArray(dst, getPackedSize());
 
     delete[] dst;
+    rsc->mHal.funcs.allocation.unlock1D(rsc, this);
 }
 
 void Allocation::serialize(Context *rsc, OStream *stream) const {
@@ -324,7 +326,8 @@
     stream->addU32(packedSize);
     if (dataSize == packedSize) {
         // Now write the data
-        stream->addByteArray(getPtr(), dataSize);
+        stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
+        rsc->mHal.funcs.allocation.unlock1D(rsc, this);
     } else {
         // Now write the data
         packVec3Allocation(rsc, stream);
@@ -396,7 +399,9 @@
 }
 
 void Allocation::freeChildrenUnlocked () {
-    decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
+    void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
+    decRefs(ptr, mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
+    mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
 }
 
 bool Allocation::freeChildren() {
@@ -419,7 +424,8 @@
 
     ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
     if (dimX < oldDimX) {
-        decRefs(getPtr(), oldDimX - dimX, dimX);
+        decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
+        rsc->mHal.funcs.allocation.unlock1D(rsc, this);
     }
     rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
     setType(t.get());
@@ -476,76 +482,6 @@
 namespace android {
 namespace renderscript {
 
-static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
-
-static void mip565(const Adapter2D &out, const Adapter2D &in) {
-    uint32_t w = out.getDimX();
-    uint32_t h = out.getDimY();
-
-    for (uint32_t y=0; y < h; y++) {
-        uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
-        const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
-        const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
-
-        for (uint32_t x=0; x < w; x++) {
-            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
-            oPtr ++;
-            i1 += 2;
-            i2 += 2;
-        }
-    }
-}
-
-static void mip8888(const Adapter2D &out, const Adapter2D &in) {
-    uint32_t w = out.getDimX();
-    uint32_t h = out.getDimY();
-
-    for (uint32_t y=0; y < h; y++) {
-        uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
-        const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
-        const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
-
-        for (uint32_t x=0; x < w; x++) {
-            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
-            oPtr ++;
-            i1 += 2;
-            i2 += 2;
-        }
-    }
-}
-
-static void mip8(const Adapter2D &out, const Adapter2D &in) {
-    uint32_t w = out.getDimX();
-    uint32_t h = out.getDimY();
-
-    for (uint32_t y=0; y < h; y++) {
-        uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
-        const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
-        const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
-
-        for (uint32_t x=0; x < w; x++) {
-            *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
-            oPtr ++;
-            i1 += 2;
-            i2 += 2;
-        }
-    }
-}
-
-static void mip(const Adapter2D &out, const Adapter2D &in) {
-    switch (out.getBaseType()->getElement()->getSizeBits()) {
-    case 32:
-        mip8888(out, in);
-        break;
-    case 16:
-        mip565(out, in);
-        break;
-    case 8:
-        mip8(out, in);
-        break;
-    }
-}
-
 void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
     Allocation *a = static_cast<Allocation *>(va);
     a->sendDirty(rsc);
@@ -553,8 +489,8 @@
 }
 
 void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
-    Allocation *texAlloc = static_cast<Allocation *>(va);
-    AllocationGenerateScriptMips(rsc, texAlloc);
+    Allocation *alloc = static_cast<Allocation *>(va);
+    rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
 }
 
 void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
@@ -610,23 +546,6 @@
     a->resize2D(rsc, dimX, dimY);
 }
 
-static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
-    Context *rsc = static_cast<Context *>(con);
-    Allocation *texAlloc = static_cast<Allocation *>(va);
-    uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
-    for (uint32_t face = 0; face < numFaces; face ++) {
-        Adapter2D adapt(rsc, texAlloc);
-        Adapter2D adapt2(rsc, texAlloc);
-        adapt.setFace(face);
-        adapt2.setFace(face);
-        for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
-            adapt.setLOD(lod);
-            adapt2.setLOD(lod + 1);
-            mip(adapt2, adapt);
-        }
-    }
-}
-
 RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
                                        RsAllocationMipmapControl mips,
                                        uint32_t usages, uint32_t ptr) {
@@ -653,7 +572,7 @@
     texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
                    t->getDimX(), t->getDimY(), data, sizeBytes);
     if (mips == RS_ALLOCATION_MIPMAP_FULL) {
-        AllocationGenerateScriptMips(rsc, texAlloc);
+        rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
     }
 
     texAlloc->sendDirty(rsc);
@@ -691,7 +610,7 @@
     }
 
     if (mips == RS_ALLOCATION_MIPMAP_FULL) {
-        AllocationGenerateScriptMips(rsc, texAlloc);
+        rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
     }
 
     texAlloc->sendDirty(rsc);
diff --git a/rsAllocation.h b/rsAllocation.h
index 9296654..5a945f1 100644
--- a/rsAllocation.h
+++ b/rsAllocation.h
@@ -82,7 +82,6 @@
     virtual ~Allocation();
     void updateCache();
 
-    void * getPtr() const {return mHal.drvState.mallocPtrLOD0;}
     const Type * getType() const {return mHal.state.type;}
 
     void syncAll(Context *rsc, RsAllocationUsageType src);
diff --git a/rsFont.cpp b/rsFont.cpp
index 1f53c79..82fb90f 100644
--- a/rsFont.cpp
+++ b/rsFont.cpp
@@ -118,7 +118,7 @@
 
     FontState *state = &mRSC->mStateFont;
     uint32_t cacheWidth = state->getCacheTextureType()->getDimX();
-    const uint8_t* cacheBuffer = state->getTextTextureData();
+    const uint8_t* cacheBuffer = state->mCacheBuffer;
 
     uint32_t cacheX = 0, cacheY = 0;
     int32_t bX = 0, bY = 0;
@@ -453,7 +453,7 @@
 
     uint32_t cacheWidth = getCacheTextureType()->getDimX();
 
-    uint8_t *cacheBuffer = (uint8_t*)mTextTexture->getPtr();
+    uint8_t *cacheBuffer = mCacheBuffer;
     uint8_t *bitmapBuffer = bitmap->buffer;
 
     uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0;
@@ -467,7 +467,10 @@
     // This will dirty the texture and the shader so next time
     // we draw it will upload the data
 
-    mTextTexture->sendDirty(mRSC);
+    mRSC->mHal.funcs.allocation.data2D(mRSC, mTextTexture.get(), 0, 0, 0,
+        RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, mCacheWidth, mCacheHeight,
+        mCacheBuffer, mCacheWidth*mCacheHeight);
+
     mFontShaderF->bindTexture(mRSC, 0, mTextTexture.get());
 
     // Some debug code
@@ -539,13 +542,16 @@
                                                                 RS_KIND_PIXEL_A, true, 1);
 
     // We will allocate a texture to initially hold 32 character bitmaps
+    mCacheHeight = 256;
+    mCacheWidth = 1024;
     ObjectBaseRef<Type> texType = Type::getTypeRef(mRSC, alphaElem.get(),
-                                                   1024, 256, 0, false, false);
+                                                   mCacheWidth, mCacheHeight, 0, false, false);
+    mCacheBuffer = new uint8_t[mCacheWidth * mCacheHeight];
+
 
     Allocation *cacheAlloc = Allocation::createAllocation(mRSC, texType.get(),
-                                RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE);
+                                RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE);
     mTextTexture.set(cacheAlloc);
-    mTextTexture->syncAll(mRSC, RS_ALLOCATION_USAGE_SCRIPT);
 
     // Split up our cache texture into lines of certain widths
     int32_t nextLine = 0;
@@ -574,7 +580,7 @@
     Allocation *indexAlloc = Allocation::createAllocation(mRSC, indexType.get(),
                                                           RS_ALLOCATION_USAGE_SCRIPT |
                                                           RS_ALLOCATION_USAGE_GRAPHICS_VERTEX);
-    uint16_t *indexPtr = (uint16_t*)indexAlloc->getPtr();
+    uint16_t *indexPtr = (uint16_t*)mRSC->mHal.funcs.allocation.lock1D(mRSC, indexAlloc);
 
     // Four verts, two triangles , six indices per quad
     for (uint32_t i = 0; i < mMaxNumberOfQuads; i ++) {
@@ -606,12 +612,14 @@
 
     Allocation *vertexAlloc = Allocation::createAllocation(mRSC, vertexDataType.get(),
                                                            RS_ALLOCATION_USAGE_SCRIPT);
-    mTextMeshPtr = (float*)vertexAlloc->getPtr();
+    mTextMeshPtr = (float*)mRSC->mHal.funcs.allocation.lock1D(mRSC, vertexAlloc);
 
     mMesh.set(new Mesh(mRSC, 1, 1));
     mMesh->setVertexBuffer(vertexAlloc, 0);
     mMesh->setPrimitive(indexAlloc, RS_PRIMITIVE_TRIANGLE, 0);
     mMesh->init();
+    mRSC->mHal.funcs.allocation.unlock1D(mRSC, indexAlloc);
+    mRSC->mHal.funcs.allocation.unlock1D(mRSC, vertexAlloc);
 }
 
 // We don't want to allocate anything unless we actually draw text
diff --git a/rsFont.h b/rsFont.h
index 460dfde..8f43a2a 100644
--- a/rsFont.h
+++ b/rsFont.h
@@ -215,10 +215,11 @@
 
     // Texture to cache glyph bitmaps
     ObjectBaseRef<Allocation> mTextTexture;
+    uint8_t *mCacheBuffer;
+    uint32_t mCacheWidth;
+    uint32_t mCacheHeight;
+
     void initTextTexture();
-    const uint8_t* getTextTextureData() const {
-        return (uint8_t*)mTextTexture->getPtr();
-    }
 
 #ifndef ANDROID_RS_SERIALIZE
     bool cacheBitmap(FT_Bitmap_ *bitmap, uint32_t *retOriginX, uint32_t *retOriginY);
diff --git a/rsScript.cpp b/rsScript.cpp
index b199496..25ee1a0 100644
--- a/rsScript.cpp
+++ b/rsScript.cpp
@@ -93,7 +93,6 @@
     Script *s = static_cast<Script *>(vs);
     Allocation *a = static_cast<Allocation *>(va);
     s->setSlot(slot, a);
-    //ALOGE("rsi_ScriptBindAllocation %i  %p  %p", slot, a, a->getPtr());
 }
 
 void rsi_ScriptSetTimeZone(Context * rsc, RsScript vs, const char * timeZone, size_t length) {
diff --git a/rsScriptC_LibGL.cpp b/rsScriptC_LibGL.cpp
index 995cef2..6a897a3 100644
--- a/rsScriptC_LibGL.cpp
+++ b/rsScriptC_LibGL.cpp
@@ -285,9 +285,10 @@
 }
 
 void rsrDrawTextAlloc(Context *rsc, Script *sc, Allocation *a, int x, int y) {
-    const char *text = (const char *)a->getPtr();
+    const char *text = (const char *)rsc->mHal.funcs.allocation.lock1D(rsc, a);
     size_t allocSize = a->getType()->getSizeBytes();
     rsc->mStateFont.renderText(text, allocSize, x, y);
+    rsc->mHal.funcs.allocation.unlock1D(rsc, a);
 }
 
 void rsrDrawText(Context *rsc, Script *sc, const char *text, int x, int y) {
@@ -314,11 +315,12 @@
 void rsrMeasureTextAlloc(Context *rsc, Script *sc, Allocation *a,
                          int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) {
     CHECK_OBJ(a);
-    const char *text = (const char *)a->getPtr();
+    const char *text = (const char *)rsc->mHal.funcs.allocation.lock1D(rsc, a);
     size_t textLen = a->getType()->getSizeBytes();
     Font::Rect metrics;
     rsc->mStateFont.measureText(text, textLen, &metrics);
     SetMetrics(&metrics, left, right, top, bottom);
+    rsc->mHal.funcs.allocation.unlock1D(rsc, a);
 }
 
 void rsrMeasureText(Context *rsc, Script *sc, const char *text,
diff --git a/rs_hal.h b/rs_hal.h
index 5903805..08938a1 100644
--- a/rs_hal.h
+++ b/rs_hal.h
@@ -186,7 +186,7 @@
         void (*elementData2D)(const Context *rsc, const Allocation *alloc, uint32_t x, uint32_t y,
                               const void *data, uint32_t elementOff, size_t sizeBytes);
 
-
+        void (*generateMipmaps)(const Context *rsc, const Allocation *alloc);
     } allocation;
 
     struct {