rsAllocation.h revision 7ac2a4dda4d20ca1f1b714e129a3a08f63178c18
1/* 2 * Copyright (C) 2009-2012 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 22struct ANativeWindow; 23 24// --------------------------------------------------------------------------- 25namespace android { 26namespace renderscript { 27 28class Program; 29 30/***************************************************************************** 31 * CAUTION 32 * 33 * Any layout changes for this class may require a corresponding change to be 34 * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains 35 * a partial copy of the information below. 36 * 37 *****************************************************************************/ 38class Allocation : public ObjectBase { 39 // The graphics equivalent of malloc. The allocation contains a structure of elements. 40 41public: 42 struct Hal { 43 void * drv; 44 45 struct State { 46 const Type * type; 47 48 uint32_t usageFlags; 49 RsAllocationMipmapControl mipmapControl; 50 51 // Cached fields from the Type and Element 52 // to prevent pointer chasing in critical loops. 53 uint32_t dimensionX; 54 uint32_t dimensionY; 55 uint32_t dimensionZ; 56 uint32_t elementSizeBytes; 57 bool hasMipmaps; 58 bool hasFaces; 59 bool hasReferences; 60 void * usrPtr; 61 int32_t surfaceTextureID; 62 ANativeWindow *wndSurface; 63 }; 64 State state; 65 66 struct DrvState { 67 void * mallocPtr; 68 } drvState; 69 70 }; 71 Hal mHal; 72 73 static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages, 74 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE, 75 void *ptr = 0); 76 virtual ~Allocation(); 77 void updateCache(); 78 79 void * getPtr() const {return mHal.drvState.mallocPtr;} 80 const Type * getType() const {return mHal.state.type;} 81 82 void syncAll(Context *rsc, RsAllocationUsageType src); 83 84 void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len); 85 86 void resize1D(Context *rsc, uint32_t dimX); 87 void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY); 88 89 void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes); 90 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 91 uint32_t w, uint32_t h, const void *data, size_t sizeBytes); 92 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face, 93 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes); 94 95 void elementData(Context *rsc, uint32_t x, 96 const void *data, uint32_t elementOff, size_t sizeBytes); 97 void elementData(Context *rsc, uint32_t x, uint32_t y, 98 const void *data, uint32_t elementOff, size_t sizeBytes); 99 100 void read(void *data); 101 102 void addProgramToDirty(const Program *); 103 void removeProgramToDirty(const Program *); 104 105 virtual void dumpLOGV(const char *prefix) const; 106 virtual void serialize(OStream *stream) const; 107 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; } 108 static Allocation *createFromStream(Context *rsc, IStream *stream); 109 110 bool getIsScript() const { 111 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0; 112 } 113 bool getIsTexture() const { 114 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0; 115 } 116 bool getIsRenderTarget() const { 117 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0; 118 } 119 bool getIsBufferObject() const { 120 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0; 121 } 122 123 void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const; 124 void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const; 125 virtual bool freeChildren(); 126 127 void sendDirty(const Context *rsc) const; 128 bool getHasGraphicsMipmaps() const { 129 return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE; 130 } 131 132 int32_t getSurfaceTextureID(const Context *rsc); 133 void setSurface(const Context *rsc, RsNativeWindow sur); 134 void ioSend(const Context *rsc); 135 void ioReceive(const Context *rsc); 136 137protected: 138 Vector<const Program *> mToDirtyList; 139 ObjectBaseRef<const Type> mType; 140 void setType(const Type *t) { 141 mType.set(t); 142 mHal.state.type = t; 143 } 144 145private: 146 void freeChildrenUnlocked(); 147 Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr); 148 149 uint32_t getPackedSize() const; 150 static void writePackedData(const Type *type, uint8_t *dst, const uint8_t *src, bool dstPadded); 151 void unpackVec3Allocation(const void *data, size_t dataSize); 152 void packVec3Allocation(OStream *stream) const; 153}; 154 155} 156} 157#endif 158 159