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 #include <vector> 23 24 struct AHardwareBuffer; 25 26 // --------------------------------------------------------------------------- 27 namespace android { 28 29 namespace renderscript { 30 31 class Program; 32 class GrallocConsumer; 33 34 /***************************************************************************** 35 * CAUTION 36 * 37 * Any layout changes for this class may require a corresponding change to be 38 * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains 39 * a partial copy of the information below. 40 * 41 *****************************************************************************/ 42 class Allocation : public ObjectBase { 43 // The graphics equivalent of malloc. The allocation contains a structure of elements. 44 45 public: 46 const static int MAX_LOD = 16; 47 // The mininum alignment requirement for RenderScript. Must be power of 2 and larger than 0. 48 const static size_t kMinimumRSAlignment = 16; 49 // The maximun number of Allocations that can share a single BufferQueue; 50 const static uint32_t MAX_NUM_ALLOC = 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 AHardwareBuffer *nativeBuffer; 71 int64_t timestamp; 72 73 // Allocation adapter state 74 const Allocation *baseAlloc; 75 uint32_t originX; 76 uint32_t originY; 77 uint32_t originZ; 78 uint32_t originLOD; 79 uint32_t originFace; 80 uint32_t originArray[Type::mMaxArrays]; 81 }; 82 State state; 83 84 struct DrvState { 85 struct LodState { 86 void * mallocPtr; 87 size_t stride; 88 uint32_t dimX; 89 uint32_t dimY; 90 uint32_t dimZ; 91 } lod[android::renderscript::Allocation::MAX_LOD]; 92 size_t faceOffset; 93 uint32_t lodCount; 94 uint32_t faceCount; 95 96 struct YuvState { 97 uint32_t shift; 98 uint32_t step; 99 } yuv; 100 101 int grallocFlags; 102 uint32_t dimArray[Type::mMaxArrays]; 103 }; 104 mutable DrvState drvState; 105 106 }; 107 Hal mHal; 108 109 void operator delete(void* ptr); 110 111 static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages, 112 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE, 113 void *ptr = 0); 114 static Allocation * createAllocationStrided(Context *rsc, const Type *, uint32_t usages, 115 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE, 116 void *ptr = 0, size_t byteAligned = 16); 117 static Allocation * createAdapter(Context *rsc, const Allocation *alloc, const Type *type); 118 119 120 virtual ~Allocation(); 121 void updateCache(); 122 123 const Type * getType() const {return mHal.state.type;} 124 125 void syncAll(Context *rsc, RsAllocationUsageType src); 126 127 void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len); 128 129 void resize1D(Context *rsc, uint32_t dimX); 130 void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY); 131 132 void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes); 133 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 134 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride); 135 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, 136 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride); 137 138 void read(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes); 139 void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 140 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride); 141 void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, 142 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride); 143 144 void elementData(Context *rsc, uint32_t x, uint32_t y, uint32_t z, 145 const void *data, uint32_t elementOff, size_t sizeBytes); 146 147 void elementRead(Context *rsc, uint32_t x, uint32_t y, uint32_t z, 148 void *data, uint32_t elementOff, size_t sizeBytes); 149 150 void addProgramToDirty(const Program *); 151 void removeProgramToDirty(const Program *); 152 153 virtual void dumpLOGV(const char *prefix) const; 154 virtual void serialize(Context *rsc, OStream *stream) const; 155 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; } 156 static Allocation *createFromStream(Context *rsc, IStream *stream); 157 158 bool getIsScript() const { 159 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0; 160 } 161 bool getIsTexture() const { 162 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0; 163 } 164 bool getIsRenderTarget() const { 165 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0; 166 } 167 bool getIsBufferObject() const { 168 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0; 169 } 170 171 void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const; 172 void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const; 173 virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const; 174 virtual bool freeChildren(); 175 176 void sendDirty(const Context *rsc) const; 177 bool getHasGraphicsMipmaps() const { 178 return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE; 179 } 180 181 void setupGrallocConsumer(const Context *rsc, uint32_t numAlloc); 182 void shareBufferQueue(const Context *rsc, const Allocation *alloc); 183 void * getSurface(const Context *rsc); 184 void setSurface(const Context *rsc, RsNativeWindow sur); 185 void ioSend(const Context *rsc); 186 void ioReceive(const Context *rsc); 187 int64_t getTimeStamp() {return mHal.state.timestamp;} 188 189 void adapterOffset(Context *rsc, const uint32_t *offsets, size_t len); 190 191 void * getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face, 192 uint32_t z, uint32_t array, size_t *stride); 193 194 void * getPointerUnchecked(uint32_t x, uint32_t y, 195 uint32_t z = 0, uint32_t lod = 0, 196 RsAllocationCubemapFace face = RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 197 uint32_t a1 = 0, uint32_t a2 = 0, uint32_t a3 = 0, uint32_t a4 = 0) const { 198 199 uint8_t * p = (uint8_t *) mHal.drvState.lod[lod].mallocPtr; 200 p += x * getType()->getElementSizeBytes(); 201 p += y * mHal.drvState.lod[lod].stride; 202 p += z * mHal.drvState.lod[lod].stride * mHal.drvState.lod[lod].dimY; 203 204 // Todo: arrays 205 206 return p; 207 } 208 209 bool hasSameDims(const Allocation *Other) const; 210 211 protected: 212 std::vector<const Program *> mToDirtyList; 213 ObjectBaseRef<const Type> mType; 214 void setType(const Type *t) { 215 mType.set(t); 216 mHal.state.type = t; 217 } 218 219 #ifndef RS_COMPATIBILITY_LIB 220 GrallocConsumer *mGrallocConsumer = nullptr; 221 bool mBufferQueueInited = false; 222 uint32_t mCurrentIdx; 223 #endif 224 225 226 private: 227 void freeChildrenUnlocked(); 228 Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr); 229 Allocation(Context *rsc, const Allocation *, const Type *); 230 231 uint32_t getPackedSize() const; 232 static void writePackedData(Context *rsc, const Type *type, uint8_t *dst, 233 const uint8_t *src, bool dstPadded); 234 void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize); 235 void packVec3Allocation(Context *rsc, OStream *stream) const; 236 }; 237 238 } // namespace renderscript 239 } // namespace android 240 #endif 241