blob: d3348410fb8ead58d9998ce77d342e5914990567 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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
Jason Sams83f1c632009-10-26 15:19:28 -070026class Program;
Jason Samsd19f10d2009-05-22 14:03:28 -070027
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080028class Allocation : public ObjectBase {
Jason Samsd19f10d2009-05-22 14:03:28 -070029 // The graphics equilivent of malloc. The allocation contains a structure of elements.
30
Jason Samsd19f10d2009-05-22 14:03:28 -070031public:
Jason Samse4a06c52011-03-16 16:29:28 -070032 struct Hal {
33 void * drv;
34
35 struct State {
36 ObjectBaseRef<const Type> type;
37 void * mallocPtr;
38
39 uint32_t usageFlags;
40 RsAllocationMipmapControl mipmapControl;
41
42 // Cached fields from the Type and Element
43 // to prevent pointer chasing in critical loops.
44 uint32_t dimensionX;
45 uint32_t dimensionY;
46 uint32_t dimensionZ;
47 uint32_t elementSizeBytes;
48 bool hasMipmaps;
49 bool hasFaces;
50 bool hasReferences;
51 };
52 State state;
53 };
54 Hal mHal;
55
Alex Sakhartchouk08571962010-12-15 09:59:58 -080056 Allocation(Context *rsc, const Type *, uint32_t usages,
57 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE);
Jason Sams8a647432010-03-01 15:31:04 -080058
Jason Samsd19f10d2009-05-22 14:03:28 -070059 virtual ~Allocation();
Jason Samse4a06c52011-03-16 16:29:28 -070060 void updateCache();
Jason Samsd19f10d2009-05-22 14:03:28 -070061
62 void setCpuWritable(bool);
63 void setGpuWritable(bool);
64 void setCpuReadable(bool);
65 void setGpuReadable(bool);
66
67 bool fixAllocation();
68
Jason Samse4a06c52011-03-16 16:29:28 -070069 void * getPtr() const {return mHal.state.mallocPtr;}
70 const Type * getType() const {return mHal.state.type.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -070071
Jason Sams5476b452010-12-08 16:14:36 -080072 void syncAll(Context *rsc, RsAllocationUsageType src);
73
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070074 void deferredUploadToTexture(const Context *rsc);
Jason Sams3b7d39b2009-12-14 12:57:40 -080075 void uploadToTexture(const Context *rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -070076 uint32_t getTextureID() const {return mTextureID;}
77
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070078 void deferredAllocateRenderTarget(const Context *rsc);
79 void allocateRenderTarget(const Context *rsc);
80 uint32_t getRenderTargetID() const {return mRenderTargetID;}
81
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080082 uint32_t getGLTarget() const;
83
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070084 void deferredUploadToBufferObject(const Context *rsc);
Jason Sams3b7d39b2009-12-14 12:57:40 -080085 void uploadToBufferObject(const Context *rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -070086 uint32_t getBufferObjectID() const {return mBufferID;}
87
Jason Sams5edc6082010-10-05 13:32:49 -070088 void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
89
90 void resize1D(Context *rsc, uint32_t dimX);
91 void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
Jason Samsd19f10d2009-05-22 14:03:28 -070092
Jason Sams49a05d72010-12-29 14:31:29 -080093 void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, uint32_t sizeBytes);
94 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Jason Sams07ae4062009-08-27 20:23:34 -070095 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes);
Jason Sams49a05d72010-12-29 14:31:29 -080096 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
Jason Sams07ae4062009-08-27 20:23:34 -070097 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -070098
Jason Sams49a05d72010-12-29 14:31:29 -080099 void elementData(Context *rsc, uint32_t x,
Jason Sams49bdaf02010-08-31 13:50:42 -0700100 const void *data, uint32_t elementOff, uint32_t sizeBytes);
Jason Sams49a05d72010-12-29 14:31:29 -0800101 void elementData(Context *rsc, uint32_t x, uint32_t y,
Jason Sams49bdaf02010-08-31 13:50:42 -0700102 const void *data, uint32_t elementOff, uint32_t sizeBytes);
103
Jason Sams40a29e82009-08-10 14:55:26 -0700104 void read(void *data);
105
Jason Sams1bada8c2009-08-09 17:01:55 -0700106 void enableGLVertexBuffers() const;
107 void setupGLIndexBuffers() const;
108
Jason Sams83f1c632009-10-26 15:19:28 -0700109 void addProgramToDirty(const Program *);
110 void removeProgramToDirty(const Program *);
Jason Sams1bada8c2009-08-09 17:01:55 -0700111
Jason Sams715333b2009-11-17 17:26:46 -0800112 virtual void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700113 virtual void serialize(OStream *stream) const;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700114 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700115 static Allocation *createFromStream(Context *rsc, IStream *stream);
Jason Sams715333b2009-11-17 17:26:46 -0800116
Jason Sams5476b452010-12-08 16:14:36 -0800117 virtual void uploadCheck(Context *rsc);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800118
Jason Sams5e0035a2010-12-13 17:11:21 -0800119 bool getIsScript() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700120 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
Jason Sams5e0035a2010-12-13 17:11:21 -0800121 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800122 bool getIsTexture() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700123 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
Jason Samsd4b23b52010-12-13 15:32:35 -0800124 }
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700125 bool getIsRenderTarget() const {
126 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
127 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800128 bool getIsBufferObject() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700129 return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
Jason Samsd4b23b52010-12-13 15:32:35 -0800130 }
Jason Samseeeaccc2010-06-25 12:45:41 -0700131
Jason Sams5edc6082010-10-05 13:32:49 -0700132 void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
133 void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700134
Jason Sams83f1c632009-10-26 15:19:28 -0700135 void sendDirty() const;
Jason Sams5e0035a2010-12-13 17:11:21 -0800136 bool getHasGraphicsMipmaps() const {
Jason Samse4a06c52011-03-16 16:29:28 -0700137 return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
Jason Sams5e0035a2010-12-13 17:11:21 -0800138 }
139
Jason Sams83f1c632009-10-26 15:19:28 -0700140
Alex Sakhartchouk8e954662010-09-01 16:34:48 -0700141protected:
Jason Sams83f1c632009-10-26 15:19:28 -0700142 Vector<const Program *> mToDirtyList;
143
Jason Sams8a647432010-03-01 15:31:04 -0800144 // Is we have a non-null user bitmap callback we do not own the bits and
145 // instead call this function to free the memort when its time.
146 RsBitmapCallback_t mUserBitmapCallback;
147 void *mUserBitmapCallbackData;
148
Jason Samsd19f10d2009-05-22 14:03:28 -0700149 // Usage restrictions
150 bool mCpuWrite;
151 bool mCpuRead;
152 bool mGpuWrite;
153 bool mGpuRead;
154
155 // more usage hint data from the application
156 // which can be used by a driver to pick the best memory type.
157 // Likely ignored for now
158 float mReadWriteRatio;
159 float mUpdateSize;
160
161
162 // Is this a legal structure to be used as a texture source.
163 // Initially this will require 1D or 2D and color data
Jason Samsd19f10d2009-05-22 14:03:28 -0700164 uint32_t mTextureID;
165
166 // Is this a legal structure to be used as a vertex source.
167 // Initially this will require 1D and x(yzw). Additional per element data
168 // is allowed.
Jason Samsd19f10d2009-05-22 14:03:28 -0700169 uint32_t mBufferID;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800170
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700171 // Is this a legal structure to be used as an FBO render target
172 uint32_t mRenderTargetID;
173
174 bool mUploadDeferred;
Jason Sams8a647432010-03-01 15:31:04 -0800175
176private:
177 void init(Context *rsc, const Type *);
Jason Samsef70a202011-01-13 17:38:18 -0800178 void upload2DTexture(bool isFirstUpload);
179 void update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
180 uint32_t lod, RsAllocationCubemapFace face, uint32_t w, uint32_t h);
Jason Sams5e0035a2010-12-13 17:11:21 -0800181
182 void allocScriptMemory();
183 void freeScriptMemory();
184
Jason Samsd19f10d2009-05-22 14:03:28 -0700185};
186
187}
188}
189#endif
190