rsAllocation.h revision b89b0b7dd8199967502c92fe5c8f57c3bc255e1c
1/* 2 * Copyright (C) 2009 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// --------------------------------------------------------------------------- 23namespace android { 24namespace renderscript { 25 26class Program; 27 28class Allocation : public ObjectBase { 29 // The graphics equilivent of malloc. The allocation contains a structure of elements. 30 31public: 32 Allocation(Context *rsc, const Type *, uint32_t usages); 33 34 virtual ~Allocation(); 35 36 void setCpuWritable(bool); 37 void setGpuWritable(bool); 38 void setCpuReadable(bool); 39 void setGpuReadable(bool); 40 41 bool fixAllocation(); 42 43 void * getPtr() const {return mPtr;} 44 const Type * getType() const {return mType.get();} 45 46 void syncAll(Context *rsc, RsAllocationUsageType src); 47 48 void deferedUploadToTexture(const Context *rsc); 49 void uploadToTexture(const Context *rsc); 50 uint32_t getTextureID() const {return mTextureID;} 51 52 uint32_t getGLTarget() const; 53 54 void deferedUploadToBufferObject(const Context *rsc); 55 void uploadToBufferObject(const Context *rsc); 56 uint32_t getBufferObjectID() const {return mBufferID;} 57 58 void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len); 59 60 void resize1D(Context *rsc, uint32_t dimX); 61 void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY); 62 63 void data(Context *rsc, const void *data, uint32_t sizeBytes); 64 void subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes); 65 void subData(Context *rsc, uint32_t xoff, uint32_t yoff, 66 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes); 67 void subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, 68 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes); 69 70 void subElementData(Context *rsc, uint32_t x, 71 const void *data, uint32_t elementOff, uint32_t sizeBytes); 72 void subElementData(Context *rsc, uint32_t x, uint32_t y, 73 const void *data, uint32_t elementOff, uint32_t sizeBytes); 74 75 void read(void *data); 76 77 void enableGLVertexBuffers() const; 78 void setupGLIndexBuffers() const; 79 80 void addProgramToDirty(const Program *); 81 void removeProgramToDirty(const Program *); 82 83 virtual void dumpLOGV(const char *prefix) const; 84 virtual void serialize(OStream *stream) const; 85 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; } 86 static Allocation *createFromStream(Context *rsc, IStream *stream); 87 88 virtual void uploadCheck(Context *rsc); 89 90 bool getIsScript() const { 91 return (mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0; 92 } 93 bool getIsTexture() const { 94 return (mUsageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0; 95 } 96 bool getIsBufferObject() const { 97 return (mUsageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0; 98 } 99 100 void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const; 101 void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const; 102 103 void sendDirty() const; 104 bool getHasGraphicsMipmaps() const { 105 return mMipmapControl != RS_ALLOCATION_MIPMAP_NONE; 106 } 107 108 void upload2DTexture(bool isFirstUpload, const void *ptr); 109 110protected: 111 ObjectBaseRef<const Type> mType; 112 void * mPtr; 113 114 Vector<const Program *> mToDirtyList; 115 116 // Is we have a non-null user bitmap callback we do not own the bits and 117 // instead call this function to free the memort when its time. 118 RsBitmapCallback_t mUserBitmapCallback; 119 void *mUserBitmapCallbackData; 120 121 // Usage restrictions 122 bool mCpuWrite; 123 bool mCpuRead; 124 bool mGpuWrite; 125 bool mGpuRead; 126 127 uint32_t mUsageFlags; 128 RsAllocationMipmapControl mMipmapControl; 129 130 // more usage hint data from the application 131 // which can be used by a driver to pick the best memory type. 132 // Likely ignored for now 133 float mReadWriteRatio; 134 float mUpdateSize; 135 136 137 // Is this a legal structure to be used as a texture source. 138 // Initially this will require 1D or 2D and color data 139 uint32_t mTextureID; 140 141 // Is this a legal structure to be used as a vertex source. 142 // Initially this will require 1D and x(yzw). Additional per element data 143 // is allowed. 144 uint32_t mBufferID; 145 146 bool mUploadDefered; 147 148private: 149 void init(Context *rsc, const Type *); 150 void uploadCubeTexture(bool isFirstUpload); 151 152 void allocScriptMemory(); 153 void freeScriptMemory(); 154 155}; 156 157} 158} 159#endif 160 161