rsAllocation.h revision 47a5881b8f85d65c74f2471fe6261d4cdb3dce5e
1/*
2 * Copyright (C) 2013 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#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
23#include <ui/GraphicBuffer.h>
24#include "rsGrallocConsumer.h"
25#include "gui/CpuConsumer.h"
26#include "gui/GLConsumer.h"
27#else
28struct ANativeWindowBuffer;
29#endif
30
31// ---------------------------------------------------------------------------
32namespace android {
33
34namespace renderscript {
35
36class Program;
37
38/*****************************************************************************
39 * CAUTION
40 *
41 * Any layout changes for this class may require a corresponding change to be
42 * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
43 * a partial copy of the information below.
44 *
45 *****************************************************************************/
46class Allocation : public ObjectBase {
47    // The graphics equivalent of malloc.  The allocation contains a structure of elements.
48
49public:
50    const static int MAX_LOD = 16;
51    // The mininum alignment requirement for RenderScript. Must be power of 2 and larger than 0.
52    const static size_t kMinimumRSAlignment = 16;
53
54    struct Hal {
55        void * drv;
56
57        struct State {
58            const Type * type;
59
60            uint32_t usageFlags;
61            RsAllocationMipmapControl mipmapControl;
62
63            // Cached fields from the Type and Element
64            // to prevent pointer chasing in critical loops.
65            uint32_t yuv;
66            uint32_t elementSizeBytes;
67            bool hasMipmaps;
68            bool hasFaces;
69            bool hasReferences;
70            void * userProvidedPtr;
71            int32_t surfaceTextureID;
72            ANativeWindowBuffer *nativeBuffer;
73            int64_t timestamp;
74
75            // Allocation adapter state
76            const Allocation *baseAlloc;
77            uint32_t originX;
78            uint32_t originY;
79            uint32_t originZ;
80            uint32_t originLOD;
81            uint32_t originFace;
82            uint32_t originArray[Type::mMaxArrays];
83        };
84        State state;
85
86        struct DrvState {
87            struct LodState {
88                void * mallocPtr;
89                size_t stride;
90                uint32_t dimX;
91                uint32_t dimY;
92                uint32_t dimZ;
93            } lod[android::renderscript::Allocation::MAX_LOD];
94            size_t faceOffset;
95            uint32_t lodCount;
96            uint32_t faceCount;
97
98            struct YuvState {
99                uint32_t shift;
100                uint32_t step;
101            } yuv;
102
103            int grallocFlags;
104            uint32_t dimArray[Type::mMaxArrays];
105        };
106        mutable DrvState drvState;
107
108    };
109    Hal mHal;
110
111    void operator delete(void* ptr);
112
113    static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
114                                         RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
115                                         void *ptr = 0);
116    static Allocation * createAllocationStrided(Context *rsc, const Type *, uint32_t usages,
117                                                RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
118                                                void *ptr = 0, size_t byteAligned = 16);
119    static Allocation * createAdapter(Context *rsc, const Allocation *alloc, const Type *type);
120
121
122    virtual ~Allocation();
123    void updateCache();
124
125    const Type * getType() const {return mHal.state.type;}
126
127    void syncAll(Context *rsc, RsAllocationUsageType src);
128
129    void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
130
131    void resize1D(Context *rsc, uint32_t dimX);
132    void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
133
134    void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes);
135    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
136              uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride);
137    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
138              uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride);
139
140    void read(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
141    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
142              uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride);
143    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
144              uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride);
145
146    void elementData(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
147                     const void *data, uint32_t elementOff, size_t sizeBytes);
148
149    void elementRead(Context *rsc, uint32_t x, uint32_t y, uint32_t z,
150                     void *data, uint32_t elementOff, size_t sizeBytes);
151
152    void addProgramToDirty(const Program *);
153    void removeProgramToDirty(const Program *);
154
155    virtual void dumpLOGV(const char *prefix) const;
156    virtual void serialize(Context *rsc, OStream *stream) const;
157    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
158    static Allocation *createFromStream(Context *rsc, IStream *stream);
159
160    bool getIsScript() const {
161        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
162    }
163    bool getIsTexture() const {
164        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
165    }
166    bool getIsRenderTarget() const {
167        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
168    }
169    bool getIsBufferObject() const {
170        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
171    }
172
173    void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
174    void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
175    virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
176    virtual bool freeChildren();
177
178    void sendDirty(const Context *rsc) const;
179    bool getHasGraphicsMipmaps() const {
180        return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
181    }
182
183    void * getSurface(const Context *rsc);
184    void setSurface(const Context *rsc, RsNativeWindow sur);
185    void ioSend(const Context *rsc);
186    void ioReceive(const Context *rsc);
187
188    void adapterOffset(Context *rsc, const uint32_t *offsets, size_t len);
189
190    void * getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face,
191                      uint32_t z, uint32_t array, size_t *stride);
192
193    void * getPointerUnchecked(uint32_t x, uint32_t y,
194                               uint32_t z = 0, uint32_t lod = 0,
195                               RsAllocationCubemapFace face = RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
196                               uint32_t a1 = 0, uint32_t a2 = 0, uint32_t a3 = 0, uint32_t a4 = 0) const {
197
198        uint8_t * p = (uint8_t *) mHal.drvState.lod[lod].mallocPtr;
199        p += x * getType()->getElementSizeBytes();
200        p += y * mHal.drvState.lod[lod].stride;
201        p += z * mHal.drvState.lod[lod].stride * mHal.drvState.lod[lod].dimY;
202
203        // Todo: arrays
204
205        return p;
206    }
207
208    bool hasSameDims(const Allocation *Other) const;
209
210protected:
211    Vector<const Program *> mToDirtyList;
212    ObjectBaseRef<const Type> mType;
213    void setType(const Type *t) {
214        mType.set(t);
215        mHal.state.type = t;
216    }
217
218#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
219    class NewBufferListener : public android::ConsumerBase::FrameAvailableListener {
220    public:
221        const android::renderscript::Context *rsc;
222        const android::renderscript::Allocation *alloc;
223
224        virtual void onFrameAvailable(const BufferItem& item);
225    };
226
227    sp<NewBufferListener> mBufferListener;
228    sp< GrallocConsumer > mGrallocConsumer;
229#endif
230
231
232private:
233    void freeChildrenUnlocked();
234    Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
235    Allocation(Context *rsc, const Allocation *, const Type *);
236
237    uint32_t getPackedSize() const;
238    static void writePackedData(Context *rsc, const Type *type, uint8_t *dst,
239                                const uint8_t *src, bool dstPadded);
240    void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
241    void packVec3Allocation(Context *rsc, OStream *stream) const;
242};
243
244}
245}
246#endif
247