blob: d1dcb7379f5ea2d6adf1cab80fd9d681029694f7 [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 Sams5476b452010-12-08 16:14:36 -080032 Allocation(Context *rsc, const Type *, uint32_t usages);
Jason Sams8a64743f2010-03-01 15:31:04 -080033
Jason Samsd19f10d2009-05-22 14:03:28 -070034 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
Jason Sams5476b452010-12-08 16:14:36 -080046 void syncAll(Context *rsc, RsAllocationUsageType src);
47
Jason Samsc2908e62010-02-23 17:44:28 -080048 void deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset);
Jason Sams3b7d39b2009-12-14 12:57:40 -080049 void uploadToTexture(const Context *rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -070050 uint32_t getTextureID() const {return mTextureID;}
51
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080052 uint32_t getGLTarget() const;
53
Jason Sams3b7d39b2009-12-14 12:57:40 -080054 void deferedUploadToBufferObject(const Context *rsc);
55 void uploadToBufferObject(const Context *rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -070056 uint32_t getBufferObjectID() const {return mBufferID;}
57
Jason Sams5edc6082010-10-05 13:32:49 -070058 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);
Jason Samsd19f10d2009-05-22 14:03:28 -070062
Jason Sams49bdaf02010-08-31 13:50:42 -070063 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,
Jason Sams07ae4062009-08-27 20:23:34 -070066 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -070067 void subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams07ae4062009-08-27 20:23:34 -070068 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -070069
Jason Sams49bdaf02010-08-31 13:50:42 -070070 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
Jason Sams40a29e82009-08-10 14:55:26 -070075 void read(void *data);
76
Jason Sams1bada8c2009-08-09 17:01:55 -070077 void enableGLVertexBuffers() const;
78 void setupGLIndexBuffers() const;
79
Jason Sams83f1c632009-10-26 15:19:28 -070080 void addProgramToDirty(const Program *);
81 void removeProgramToDirty(const Program *);
Jason Sams1bada8c2009-08-09 17:01:55 -070082
Jason Sams715333b2009-11-17 17:26:46 -080083 virtual void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070084 virtual void serialize(OStream *stream) const;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070085 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070086 static Allocation *createFromStream(Context *rsc, IStream *stream);
Jason Sams715333b2009-11-17 17:26:46 -080087
Jason Sams5476b452010-12-08 16:14:36 -080088 virtual void uploadCheck(Context *rsc);
Jason Sams3b7d39b2009-12-14 12:57:40 -080089
Jason Samsd4b23b52010-12-13 15:32:35 -080090 bool getIsTexture() const {
91 return (mUsageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
92 }
93 bool getIsBufferObject() const {
94 return (mUsageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
95 }
Jason Samseeeaccc2010-06-25 12:45:41 -070096
Jason Sams5edc6082010-10-05 13:32:49 -070097 void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
98 void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
Jason Samsb28ca96f2010-08-09 18:13:33 -070099
Jason Sams83f1c632009-10-26 15:19:28 -0700100 void sendDirty() const;
Jason Samsd081fff2010-09-16 18:18:29 -0700101 bool getHasGraphicsMipmaps() const {return mTextureGenMipmap;}
Jason Sams83f1c632009-10-26 15:19:28 -0700102
Alex Sakhartchouk8e954662010-09-01 16:34:48 -0700103protected:
Jason Samsd19f10d2009-05-22 14:03:28 -0700104 ObjectBaseRef<const Type> mType;
105 void * mPtr;
106
Jason Sams83f1c632009-10-26 15:19:28 -0700107 Vector<const Program *> mToDirtyList;
108
Jason Sams8a64743f2010-03-01 15:31:04 -0800109 // Is we have a non-null user bitmap callback we do not own the bits and
110 // instead call this function to free the memort when its time.
111 RsBitmapCallback_t mUserBitmapCallback;
112 void *mUserBitmapCallbackData;
113
Jason Samsd19f10d2009-05-22 14:03:28 -0700114 // Usage restrictions
115 bool mCpuWrite;
116 bool mCpuRead;
117 bool mGpuWrite;
118 bool mGpuRead;
119
Jason Sams5476b452010-12-08 16:14:36 -0800120 uint32_t mUsageFlags;
Jason Samsd4b23b52010-12-13 15:32:35 -0800121 RsAllocationMipmapControl mMipmapControl;
Jason Sams5476b452010-12-08 16:14:36 -0800122
Jason Samsd19f10d2009-05-22 14:03:28 -0700123 // more usage hint data from the application
124 // which can be used by a driver to pick the best memory type.
125 // Likely ignored for now
126 float mReadWriteRatio;
127 float mUpdateSize;
128
129
130 // Is this a legal structure to be used as a texture source.
131 // Initially this will require 1D or 2D and color data
Jason Samsc2908e62010-02-23 17:44:28 -0800132 bool mTextureGenMipmap;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800133 uint32_t mTextureLOD;
Jason Samsd19f10d2009-05-22 14:03:28 -0700134 uint32_t mTextureID;
135
136 // Is this a legal structure to be used as a vertex source.
137 // Initially this will require 1D and x(yzw). Additional per element data
138 // is allowed.
Jason Samsd19f10d2009-05-22 14:03:28 -0700139 uint32_t mBufferID;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800140
141 bool mUploadDefered;
Jason Sams8a64743f2010-03-01 15:31:04 -0800142
143private:
144 void init(Context *rsc, const Type *);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800145 void upload2DTexture(bool isFirstUpload);
146 void uploadCubeTexture(bool isFirstUpload);
Jason Samsd19f10d2009-05-22 14:03:28 -0700147};
148
149}
150}
151#endif
152