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