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 #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
     23 #include <ui/GraphicBuffer.h>
     24 #include "rsGrallocConsumer.h"
     25 #include "gui/CpuConsumer.h"
     26 #include "gui/GLConsumer.h"
     27 #else
     28 struct ANativeWindowBuffer;
     29 #endif
     30 
     31 // ---------------------------------------------------------------------------
     32 namespace android {
     33 
     34 namespace renderscript {
     35 
     36 class Program;
     37 
     38 /*****************************************************************************
     39  * CAUTION
     40  *
     41  * Any layout changes for this class may require a corresponding change to be
     42  * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
     43  * a partial copy of the information below.
     44  *
     45  *****************************************************************************/
     46 class Allocation : public ObjectBase {
     47     // The graphics equivalent of malloc.  The allocation contains a structure of elements.
     48 
     49 public:
     50     const static int MAX_LOD = 16;
     51 
     52     struct Hal {
     53         void * drv;
     54 
     55         struct State {
     56             const Type * type;
     57 
     58             uint32_t usageFlags;
     59             RsAllocationMipmapControl mipmapControl;
     60 
     61             // Cached fields from the Type and Element
     62             // to prevent pointer chasing in critical loops.
     63             uint32_t yuv;
     64             uint32_t elementSizeBytes;
     65             bool hasMipmaps;
     66             bool hasFaces;
     67             bool hasReferences;
     68             void * userProvidedPtr;
     69             int32_t surfaceTextureID;
     70             ANativeWindowBuffer *nativeBuffer;
     71             int64_t timestamp;
     72         };
     73         State state;
     74 
     75         struct DrvState {
     76             struct LodState {
     77                 void * mallocPtr;
     78                 size_t stride;
     79                 uint32_t dimX;
     80                 uint32_t dimY;
     81                 uint32_t dimZ;
     82             } lod[android::renderscript::Allocation::MAX_LOD];
     83             size_t faceOffset;
     84             uint32_t lodCount;
     85             uint32_t faceCount;
     86 
     87             struct YuvState {
     88                 uint32_t shift;
     89                 uint32_t step;
     90             } yuv;
     91         };
     92         mutable DrvState drvState;
     93 
     94     };
     95     Hal mHal;
     96 
     97     void operator delete(void* ptr);
     98 
     99     static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
    100                                          RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
    101                                          void *ptr = 0);
    102     virtual ~Allocation();
    103     void updateCache();
    104 
    105     const Type * getType() const {return mHal.state.type;}
    106 
    107     void syncAll(Context *rsc, RsAllocationUsageType src);
    108 
    109     void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
    110 
    111     void resize1D(Context *rsc, uint32_t dimX);
    112     void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
    113 
    114     void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes);
    115     void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
    116               uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride);
    117     void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
    118               uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride);
    119 
    120     void read(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
    121     void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
    122               uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride);
    123     void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
    124               uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride);
    125 
    126     void elementData(Context *rsc, uint32_t x,
    127                      const void *data, uint32_t elementOff, size_t sizeBytes);
    128     void elementData(Context *rsc, uint32_t x, uint32_t y,
    129                      const void *data, uint32_t elementOff, size_t sizeBytes);
    130 
    131     void addProgramToDirty(const Program *);
    132     void removeProgramToDirty(const Program *);
    133 
    134     virtual void dumpLOGV(const char *prefix) const;
    135     virtual void serialize(Context *rsc, OStream *stream) const;
    136     virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
    137     static Allocation *createFromStream(Context *rsc, IStream *stream);
    138 
    139     bool getIsScript() const {
    140         return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
    141     }
    142     bool getIsTexture() const {
    143         return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
    144     }
    145     bool getIsRenderTarget() const {
    146         return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
    147     }
    148     bool getIsBufferObject() const {
    149         return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
    150     }
    151 
    152     void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
    153     void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
    154     virtual bool freeChildren();
    155 
    156     void sendDirty(const Context *rsc) const;
    157     bool getHasGraphicsMipmaps() const {
    158         return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
    159     }
    160 
    161     void * getSurface(const Context *rsc);
    162     void setSurface(const Context *rsc, RsNativeWindow sur);
    163     void ioSend(const Context *rsc);
    164     void ioReceive(const Context *rsc);
    165 
    166 protected:
    167     Vector<const Program *> mToDirtyList;
    168     ObjectBaseRef<const Type> mType;
    169     void setType(const Type *t) {
    170         mType.set(t);
    171         mHal.state.type = t;
    172     }
    173 
    174 #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
    175     class NewBufferListener : public android::ConsumerBase::FrameAvailableListener {
    176     public:
    177         const android::renderscript::Context *rsc;
    178         const android::renderscript::Allocation *alloc;
    179 
    180         virtual void onFrameAvailable();
    181     };
    182 
    183     sp<NewBufferListener> mBufferListener;
    184     sp< GrallocConsumer > mGrallocConsumer;
    185 #endif
    186 
    187 
    188 private:
    189     void freeChildrenUnlocked();
    190     Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
    191 
    192     uint32_t getPackedSize() const;
    193     static void writePackedData(Context *rsc, const Type *type, uint8_t *dst,
    194                                 const uint8_t *src, bool dstPadded);
    195     void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
    196     void packVec3Allocation(Context *rsc, OStream *stream) const;
    197 };
    198 
    199 }
    200 }
    201 #endif
    202 
    203