rsAllocation.h revision bad807405b2b9764372af1ad24bcfd4fb1f33d8e
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    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
56    Allocation(Context *rsc, const Type *, uint32_t usages,
57               RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE);
58
59    virtual ~Allocation();
60    void updateCache();
61
62    void setCpuWritable(bool);
63    void setGpuWritable(bool);
64    void setCpuReadable(bool);
65    void setGpuReadable(bool);
66
67    bool fixAllocation();
68
69    void * getPtr() const {return mHal.state.mallocPtr;}
70    const Type * getType() const {return mHal.state.type.get();}
71
72    void syncAll(Context *rsc, RsAllocationUsageType src);
73
74    void deferedUploadToTexture(const Context *rsc);
75    void uploadToTexture(const Context *rsc);
76    uint32_t getTextureID() const {return mTextureID;}
77
78    uint32_t getGLTarget() const;
79
80    void deferedUploadToBufferObject(const Context *rsc);
81    void uploadToBufferObject(const Context *rsc);
82    uint32_t getBufferObjectID() const {return mBufferID;}
83
84    void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
85
86    void resize1D(Context *rsc, uint32_t dimX);
87    void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
88
89    void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, uint32_t sizeBytes);
90    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
91                 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes);
92    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
93                 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes);
94
95    void elementData(Context *rsc, uint32_t x,
96                        const void *data, uint32_t elementOff, uint32_t sizeBytes);
97    void elementData(Context *rsc, uint32_t x, uint32_t y,
98                        const void *data, uint32_t elementOff, uint32_t sizeBytes);
99
100    void read(void *data);
101
102    void enableGLVertexBuffers() const;
103    void setupGLIndexBuffers() const;
104
105    void addProgramToDirty(const Program *);
106    void removeProgramToDirty(const Program *);
107
108    virtual void dumpLOGV(const char *prefix) const;
109    virtual void serialize(OStream *stream) const;
110    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
111    static Allocation *createFromStream(Context *rsc, IStream *stream);
112
113    virtual void uploadCheck(Context *rsc);
114
115    bool getIsScript() const {
116        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
117    }
118    bool getIsTexture() const {
119        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
120    }
121    bool getIsBufferObject() const {
122        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
123    }
124
125    void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
126    void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
127
128    void sendDirty() const;
129    bool getHasGraphicsMipmaps() const {
130        return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
131    }
132
133
134protected:
135    Vector<const Program *> mToDirtyList;
136
137    // Is we have a non-null user bitmap callback we do not own the bits and
138    // instead call this function to free the memort when its time.
139    RsBitmapCallback_t mUserBitmapCallback;
140    void *mUserBitmapCallbackData;
141
142    // Usage restrictions
143    bool mCpuWrite;
144    bool mCpuRead;
145    bool mGpuWrite;
146    bool mGpuRead;
147
148    // more usage hint data from the application
149    // which can be used by a driver to pick the best memory type.
150    // Likely ignored for now
151    float mReadWriteRatio;
152    float mUpdateSize;
153
154
155    // Is this a legal structure to be used as a texture source.
156    // Initially this will require 1D or 2D and color data
157    uint32_t mTextureID;
158
159    // Is this a legal structure to be used as a vertex source.
160    // Initially this will require 1D and x(yzw).  Additional per element data
161    // is allowed.
162    uint32_t mBufferID;
163
164    bool mUploadDefered;
165
166private:
167    void init(Context *rsc, const Type *);
168    void upload2DTexture(bool isFirstUpload);
169    void update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
170                         uint32_t lod, RsAllocationCubemapFace face, uint32_t w, uint32_t h);
171
172    void allocScriptMemory();
173    void freeScriptMemory();
174
175};
176
177}
178}
179#endif
180
181