blob: a8d086eeeb4896420c56e2a5d52179a792aee4ae [file] [log] [blame]
Jason Sams326e0dd2009-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 Sams5c3e3bc2009-10-26 15:19:28 -070026class Program;
Jason Sams326e0dd2009-05-22 14:03:28 -070027
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080028class Allocation : public ObjectBase {
Jason Sams326e0dd2009-05-22 14:03:28 -070029 // The graphics equilivent of malloc. The allocation contains a structure of elements.
30
Jason Sams326e0dd2009-05-22 14:03:28 -070031public:
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080032 Allocation(Context *rsc, const Type *, uint32_t usages,
33 RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE);
Jason Samsfa84da22010-03-01 15:31:04 -080034
Jason Sams326e0dd2009-05-22 14:03:28 -070035 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 Sams366c9c82010-12-08 16:14:36 -080047 void syncAll(Context *rsc, RsAllocationUsageType src);
48
Jason Samsb89b0b72010-12-13 17:11:21 -080049 void deferedUploadToTexture(const Context *rsc);
Jason Samscf4c7c92009-12-14 12:57:40 -080050 void uploadToTexture(const Context *rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070051 uint32_t getTextureID() const {return mTextureID;}
52
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080053 uint32_t getGLTarget() const;
54
Jason Samscf4c7c92009-12-14 12:57:40 -080055 void deferedUploadToBufferObject(const Context *rsc);
56 void uploadToBufferObject(const Context *rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070057 uint32_t getBufferObjectID() const {return mBufferID;}
58
Jason Sams96abf812010-10-05 13:32:49 -070059 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 Sams326e0dd2009-05-22 14:03:28 -070063
Jason Sams4b45b892010-12-29 14:31:29 -080064 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 Sams9397e302009-08-27 20:23:34 -070066 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes);
Jason Sams4b45b892010-12-29 14:31:29 -080067 void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
Jason Sams9397e302009-08-27 20:23:34 -070068 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -070069
Jason Sams4b45b892010-12-29 14:31:29 -080070 void elementData(Context *rsc, uint32_t x,
Jason Sams5f0c84c2010-08-31 13:50:42 -070071 const void *data, uint32_t elementOff, uint32_t sizeBytes);
Jason Sams4b45b892010-12-29 14:31:29 -080072 void elementData(Context *rsc, uint32_t x, uint32_t y,
Jason Sams5f0c84c2010-08-31 13:50:42 -070073 const void *data, uint32_t elementOff, uint32_t sizeBytes);
74
Jason Samse579df42009-08-10 14:55:26 -070075 void read(void *data);
76
Jason Samse5ffb872009-08-09 17:01:55 -070077 void enableGLVertexBuffers() const;
78 void setupGLIndexBuffers() const;
79
Jason Sams5c3e3bc2009-10-26 15:19:28 -070080 void addProgramToDirty(const Program *);
81 void removeProgramToDirty(const Program *);
Jason Samse5ffb872009-08-09 17:01:55 -070082
Jason Samsc21cf402009-11-17 17:26:46 -080083 virtual void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070084 virtual void serialize(OStream *stream) const;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070085 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070086 static Allocation *createFromStream(Context *rsc, IStream *stream);
Jason Samsc21cf402009-11-17 17:26:46 -080087
Jason Sams366c9c82010-12-08 16:14:36 -080088 virtual void uploadCheck(Context *rsc);
Jason Samscf4c7c92009-12-14 12:57:40 -080089
Jason Samsb89b0b72010-12-13 17:11:21 -080090 bool getIsScript() const {
91 return (mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
92 }
Jason Samsebc50192010-12-13 15:32:35 -080093 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 Sams760f1f72010-06-25 12:45:41 -070099
Jason Sams96abf812010-10-05 13:32:49 -0700100 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 Samse3929c92010-08-09 18:13:33 -0700102
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700103 void sendDirty() const;
Jason Samsb89b0b72010-12-13 17:11:21 -0800104 bool getHasGraphicsMipmaps() const {
105 return mMipmapControl != RS_ALLOCATION_MIPMAP_NONE;
106 }
107
108 void upload2DTexture(bool isFirstUpload, const void *ptr);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700109
Alex Sakhartchouk1e5168d2010-09-01 16:34:48 -0700110protected:
Jason Sams326e0dd2009-05-22 14:03:28 -0700111 ObjectBaseRef<const Type> mType;
112 void * mPtr;
113
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700114 Vector<const Program *> mToDirtyList;
115
Jason Samsfa84da22010-03-01 15:31:04 -0800116 // 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
Jason Sams326e0dd2009-05-22 14:03:28 -0700121 // Usage restrictions
122 bool mCpuWrite;
123 bool mCpuRead;
124 bool mGpuWrite;
125 bool mGpuRead;
126
Jason Sams366c9c82010-12-08 16:14:36 -0800127 uint32_t mUsageFlags;
Jason Samsebc50192010-12-13 15:32:35 -0800128 RsAllocationMipmapControl mMipmapControl;
Jason Sams366c9c82010-12-08 16:14:36 -0800129
Jason Sams326e0dd2009-05-22 14:03:28 -0700130 // 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
Jason Sams326e0dd2009-05-22 14:03:28 -0700139 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.
Jason Sams326e0dd2009-05-22 14:03:28 -0700144 uint32_t mBufferID;
Jason Samscf4c7c92009-12-14 12:57:40 -0800145
146 bool mUploadDefered;
Jason Samsfa84da22010-03-01 15:31:04 -0800147
148private:
149 void init(Context *rsc, const Type *);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800150 void uploadCubeTexture(bool isFirstUpload);
Jason Samsb89b0b72010-12-13 17:11:21 -0800151
152 void allocScriptMemory();
153 void freeScriptMemory();
154
Jason Sams326e0dd2009-05-22 14:03:28 -0700155};
156
157}
158}
159#endif
160