rsAllocation.h revision 5f0c84cf464dda719cef65fdc9b4d0980e86b98f
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{
30    // The graphics equilivent of malloc.  The allocation contains a structure of elements.
31
32public:
33    // By policy this allocation will hold a pointer to the type
34    // but will not destroy it on destruction.
35    Allocation(Context *rsc, const Type *);
36    Allocation(Context *rsc, const Type *, void *bmp, void *callbackData, RsBitmapCallback_t callback);
37
38    virtual ~Allocation();
39
40    void setCpuWritable(bool);
41    void setGpuWritable(bool);
42    void setCpuReadable(bool);
43    void setGpuReadable(bool);
44
45    bool fixAllocation();
46
47    void * getPtr() const {return mPtr;}
48    const Type * getType() const {return mType.get();}
49
50    void deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset);
51    void uploadToTexture(const Context *rsc);
52    uint32_t getTextureID() const {return mTextureID;}
53
54    void deferedUploadToBufferObject(const Context *rsc);
55    void uploadToBufferObject(const Context *rsc);
56    uint32_t getBufferObjectID() const {return mBufferID;}
57
58
59    void data(Context *rsc, const void *data, uint32_t sizeBytes);
60    void subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes);
61    void subData(Context *rsc, uint32_t xoff, uint32_t yoff,
62                 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes);
63    void subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
64                 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
65
66    void subElementData(Context *rsc, uint32_t x,
67                        const void *data, uint32_t elementOff, uint32_t sizeBytes);
68    void subElementData(Context *rsc, uint32_t x, uint32_t y,
69                        const void *data, uint32_t elementOff, uint32_t sizeBytes);
70
71    void read(void *data);
72
73    void enableGLVertexBuffers() const;
74    void setupGLIndexBuffers() const;
75
76    void addProgramToDirty(const Program *);
77    void removeProgramToDirty(const Program *);
78
79    virtual void dumpLOGV(const char *prefix) const;
80    virtual void serialize(OStream *stream) const;
81    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
82    static Allocation *createFromStream(Context *rsc, IStream *stream);
83
84    virtual void uploadCheck(const Context *rsc);
85
86    bool getIsTexture() const {return mIsTexture;}
87    bool getIsBufferObject() const {return mIsVertexBuffer;}
88
89    void incRefs(const void *ptr, size_t ct) const;
90    void decRefs(const void *ptr, size_t ct) const;
91
92protected:
93    void sendDirty() const;
94
95    ObjectBaseRef<const Type> mType;
96    void * mPtr;
97
98    Vector<const Program *> mToDirtyList;
99
100    // Is we have a non-null user bitmap callback we do not own the bits and
101    // instead call this function to free the memort when its time.
102    RsBitmapCallback_t mUserBitmapCallback;
103    void *mUserBitmapCallbackData;
104
105    // Usage restrictions
106    bool mCpuWrite;
107    bool mCpuRead;
108    bool mGpuWrite;
109    bool mGpuRead;
110
111    // more usage hint data from the application
112    // which can be used by a driver to pick the best memory type.
113    // Likely ignored for now
114    float mReadWriteRatio;
115    float mUpdateSize;
116
117
118    // Is this a legal structure to be used as a texture source.
119    // Initially this will require 1D or 2D and color data
120    bool mIsTexture;
121    bool mTextureGenMipmap;
122    uint32_t mTextureLOD;
123    uint32_t mTextureID;
124
125    // Is this a legal structure to be used as a vertex source.
126    // Initially this will require 1D and x(yzw).  Additional per element data
127    // is allowed.
128    bool mIsVertexBuffer;
129    uint32_t mBufferID;
130
131    bool mUploadDefered;
132
133private:
134    void init(Context *rsc, const Type *);
135
136};
137
138}
139}
140#endif
141
142