Home | History | Annotate | Download | only in rs
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ANDROID_STRUCTURED_ALLOCATION_H
     18 #define ANDROID_STRUCTURED_ALLOCATION_H
     19 
     20 #include "rsType.h"
     21 
     22 // ---------------------------------------------------------------------------
     23 namespace android {
     24 
     25 namespace renderscript {
     26 
     27 class Program;
     28 
     29 /*****************************************************************************
     30  * CAUTION
     31  *
     32  * Any layout changes for this class may require a corresponding change to be
     33  * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
     34  * a partial copy of the information below.
     35  *
     36  *****************************************************************************/
     37 class Allocation : public ObjectBase {
     38     // The graphics equivalent of malloc.  The allocation contains a structure of elements.
     39 
     40 public:
     41     const static int MAX_LOD = 16;
     42 
     43     struct Hal {
     44         void * drv;
     45 
     46         struct State {
     47             const Type * type;
     48 
     49             uint32_t usageFlags;
     50             RsAllocationMipmapControl mipmapControl;
     51 
     52             // Cached fields from the Type and Element
     53             // to prevent pointer chasing in critical loops.
     54             uint32_t yuv;
     55             uint32_t elementSizeBytes;
     56             bool hasMipmaps;
     57             bool hasFaces;
     58             bool hasReferences;
     59             void * userProvidedPtr;
     60             int32_t surfaceTextureID;
     61             void *deprecated01;
     62             void *deprecated02;
     63         };
     64         State state;
     65 
     66         struct DrvState {
     67             struct LodState {
     68                 void * mallocPtr;
     69                 size_t stride;
     70                 uint32_t dimX;
     71                 uint32_t dimY;
     72                 uint32_t dimZ;
     73             } lod[android::renderscript::Allocation::MAX_LOD];
     74             size_t faceOffset;
     75             uint32_t lodCount;
     76             uint32_t faceCount;
     77         };
     78         mutable DrvState drvState;
     79 
     80     };
     81     Hal mHal;
     82 
     83     void operator delete(void* ptr);
     84 
     85     static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
     86                                          RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
     87                                          void *ptr = 0);
     88     virtual ~Allocation();
     89     void updateCache();
     90 
     91     const Type * getType() const {return mHal.state.type;}
     92 
     93     void syncAll(Context *rsc, RsAllocationUsageType src);
     94 
     95     void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
     96 
     97     void resize1D(Context *rsc, uint32_t dimX);
     98     void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
     99 
    100     void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes);
    101     void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
    102               uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride);
    103     void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
    104               uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride);
    105 
    106     void read(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
    107     void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
    108               uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride);
    109     void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
    110               uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride);
    111 
    112     void elementData(Context *rsc, uint32_t x,
    113                      const void *data, uint32_t elementOff, size_t sizeBytes);
    114     void elementData(Context *rsc, uint32_t x, uint32_t y,
    115                      const void *data, uint32_t elementOff, size_t sizeBytes);
    116 
    117     void addProgramToDirty(const Program *);
    118     void removeProgramToDirty(const Program *);
    119 
    120     virtual void dumpLOGV(const char *prefix) const;
    121     virtual void serialize(Context *rsc, OStream *stream) const;
    122     virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
    123     static Allocation *createFromStream(Context *rsc, IStream *stream);
    124 
    125     bool getIsScript() const {
    126         return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
    127     }
    128     bool getIsTexture() const {
    129         return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
    130     }
    131     bool getIsRenderTarget() const {
    132         return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
    133     }
    134     bool getIsBufferObject() const {
    135         return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
    136     }
    137 
    138     void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
    139     void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
    140     virtual bool freeChildren();
    141 
    142     void sendDirty(const Context *rsc) const;
    143     bool getHasGraphicsMipmaps() const {
    144         return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
    145     }
    146 
    147     void * getSurface(const Context *rsc);
    148     void setSurface(const Context *rsc, RsNativeWindow sur);
    149     void ioSend(const Context *rsc);
    150     void ioReceive(const Context *rsc);
    151 
    152 protected:
    153     Vector<const Program *> mToDirtyList;
    154     ObjectBaseRef<const Type> mType;
    155     void setType(const Type *t) {
    156         mType.set(t);
    157         mHal.state.type = t;
    158     }
    159 
    160 private:
    161     void freeChildrenUnlocked();
    162     Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
    163 
    164     uint32_t getPackedSize() const;
    165     static void writePackedData(Context *rsc, const Type *type, uint8_t *dst,
    166                                 const uint8_t *src, bool dstPadded);
    167     void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
    168     void packVec3Allocation(Context *rsc, OStream *stream) const;
    169 };
    170 
    171 }
    172 }
    173 #endif
    174 
    175