Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | // --------------------------------------------------------------------------- |
| 23 | namespace android { |
| 24 | namespace renderscript { |
| 25 | |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 26 | class Program; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 28 | class Allocation : public ObjectBase { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 29 | // The graphics equilivent of malloc. The allocation contains a structure of elements. |
| 30 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | public: |
Alex Sakhartchouk | 0857196 | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 32 | Allocation(Context *rsc, const Type *, uint32_t usages, |
| 33 | RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE); |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 34 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 35 | virtual ~Allocation(); |
| 36 | |
| 37 | void setCpuWritable(bool); |
| 38 | void setGpuWritable(bool); |
| 39 | void setCpuReadable(bool); |
| 40 | void setGpuReadable(bool); |
| 41 | |
| 42 | bool fixAllocation(); |
| 43 | |
| 44 | void * getPtr() const {return mPtr;} |
| 45 | const Type * getType() const {return mType.get();} |
| 46 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 47 | void syncAll(Context *rsc, RsAllocationUsageType src); |
| 48 | |
Jason Sams | 5e0035a | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 49 | void deferedUploadToTexture(const Context *rsc); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 50 | void uploadToTexture(const Context *rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | uint32_t getTextureID() const {return mTextureID;} |
| 52 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 53 | uint32_t getGLTarget() const; |
| 54 | |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 55 | void deferedUploadToBufferObject(const Context *rsc); |
| 56 | void uploadToBufferObject(const Context *rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 57 | uint32_t getBufferObjectID() const {return mBufferID;} |
| 58 | |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 59 | void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len); |
| 60 | |
| 61 | void resize1D(Context *rsc, uint32_t dimX); |
| 62 | void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 63 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 64 | void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, uint32_t sizeBytes); |
| 65 | void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 66 | uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 67 | void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face, |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 68 | uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 69 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 70 | void elementData(Context *rsc, uint32_t x, |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 71 | const void *data, uint32_t elementOff, uint32_t sizeBytes); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 72 | void elementData(Context *rsc, uint32_t x, uint32_t y, |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 73 | const void *data, uint32_t elementOff, uint32_t sizeBytes); |
| 74 | |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 75 | void read(void *data); |
| 76 | |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 77 | void enableGLVertexBuffers() const; |
| 78 | void setupGLIndexBuffers() const; |
| 79 | |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 80 | void addProgramToDirty(const Program *); |
| 81 | void removeProgramToDirty(const Program *); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 82 | |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 83 | virtual void dumpLOGV(const char *prefix) const; |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 84 | virtual void serialize(OStream *stream) const; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 85 | virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; } |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 86 | static Allocation *createFromStream(Context *rsc, IStream *stream); |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 87 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 88 | virtual void uploadCheck(Context *rsc); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 89 | |
Jason Sams | 5e0035a | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 90 | bool getIsScript() const { |
| 91 | return (mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0; |
| 92 | } |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 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 | } |
Jason Sams | eeeaccc | 2010-06-25 12:45:41 -0700 | [diff] [blame] | 99 | |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 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; |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 102 | |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 103 | void sendDirty() const; |
Jason Sams | 5e0035a | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 104 | bool getHasGraphicsMipmaps() const { |
| 105 | return mMipmapControl != RS_ALLOCATION_MIPMAP_NONE; |
| 106 | } |
| 107 | |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 108 | |
Alex Sakhartchouk | 8e95466 | 2010-09-01 16:34:48 -0700 | [diff] [blame] | 109 | protected: |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 110 | ObjectBaseRef<const Type> mType; |
| 111 | void * mPtr; |
| 112 | |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 113 | Vector<const Program *> mToDirtyList; |
| 114 | |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 115 | // Is we have a non-null user bitmap callback we do not own the bits and |
| 116 | // instead call this function to free the memort when its time. |
| 117 | RsBitmapCallback_t mUserBitmapCallback; |
| 118 | void *mUserBitmapCallbackData; |
| 119 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 120 | // Usage restrictions |
| 121 | bool mCpuWrite; |
| 122 | bool mCpuRead; |
| 123 | bool mGpuWrite; |
| 124 | bool mGpuRead; |
| 125 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 126 | uint32_t mUsageFlags; |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 127 | RsAllocationMipmapControl mMipmapControl; |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 128 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 129 | // more usage hint data from the application |
| 130 | // which can be used by a driver to pick the best memory type. |
| 131 | // Likely ignored for now |
| 132 | float mReadWriteRatio; |
| 133 | float mUpdateSize; |
| 134 | |
| 135 | |
| 136 | // Is this a legal structure to be used as a texture source. |
| 137 | // Initially this will require 1D or 2D and color data |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 138 | uint32_t mTextureID; |
| 139 | |
| 140 | // Is this a legal structure to be used as a vertex source. |
| 141 | // Initially this will require 1D and x(yzw). Additional per element data |
| 142 | // is allowed. |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 143 | uint32_t mBufferID; |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 144 | |
| 145 | bool mUploadDefered; |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 146 | |
| 147 | private: |
| 148 | void init(Context *rsc, const Type *); |
Jason Sams | ef70a20 | 2011-01-13 17:38:18 -0800 | [diff] [blame] | 149 | void upload2DTexture(bool isFirstUpload); |
| 150 | void update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff, |
| 151 | uint32_t lod, RsAllocationCubemapFace face, uint32_t w, uint32_t h); |
Jason Sams | 5e0035a | 2010-12-13 17:11:21 -0800 | [diff] [blame] | 152 | |
| 153 | void allocScriptMemory(); |
| 154 | void freeScriptMemory(); |
| 155 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | } |
| 159 | } |
| 160 | #endif |
| 161 | |