rsAllocation.h revision ddceab9a001f07a3395226c5e06e3b420720af0f
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#include <ui/GraphicBuffer.h>
23
24#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
25#include "rsGrallocConsumer.h"
26#include "gui/CpuConsumer.h"
27#include "gui/GLConsumer.h"
28#endif
29
30// ---------------------------------------------------------------------------
31namespace android {
32
33namespace renderscript {
34
35class Program;
36
37/*****************************************************************************
38 * CAUTION
39 *
40 * Any layout changes for this class may require a corresponding change to be
41 * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
42 * a partial copy of the information below.
43 *
44 *****************************************************************************/
45class Allocation : public ObjectBase {
46    // The graphics equivalent of malloc.  The allocation contains a structure of elements.
47
48public:
49    const static int MAX_LOD = 16;
50
51    struct Hal {
52        void * drv;
53
54        struct State {
55            const Type * type;
56
57            uint32_t usageFlags;
58            RsAllocationMipmapControl mipmapControl;
59
60            // Cached fields from the Type and Element
61            // to prevent pointer chasing in critical loops.
62            uint32_t yuv;
63            uint32_t elementSizeBytes;
64            bool hasMipmaps;
65            bool hasFaces;
66            bool hasReferences;
67            void * userProvidedPtr;
68            int32_t surfaceTextureID;
69            ANativeWindowBuffer *nativeBuffer;
70            int64_t timestamp;
71        };
72        State state;
73
74        struct DrvState {
75            struct LodState {
76                void * mallocPtr;
77                size_t stride;
78                uint32_t dimX;
79                uint32_t dimY;
80                uint32_t dimZ;
81            } lod[android::renderscript::Allocation::MAX_LOD];
82            size_t faceOffset;
83            uint32_t lodCount;
84            uint32_t faceCount;
85        };
86        mutable DrvState drvState;
87
88    };
89    Hal mHal;
90
91    void operator delete(void* ptr);
92
93    static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
94                                         RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
95                                         void *ptr = 0);
96    virtual ~Allocation();
97    void updateCache();
98
99    const Type * getType() const {return mHal.state.type;}
100
101    void syncAll(Context *rsc, RsAllocationUsageType src);
102
103    void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
104
105    void resize1D(Context *rsc, uint32_t dimX);
106    void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
107
108    void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes);
109    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
110              uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride);
111    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
112              uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride);
113
114    void read(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
115    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
116              uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride);
117    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
118              uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride);
119
120    void elementData(Context *rsc, uint32_t x,
121                     const void *data, uint32_t elementOff, size_t sizeBytes);
122    void elementData(Context *rsc, uint32_t x, uint32_t y,
123                     const void *data, uint32_t elementOff, size_t sizeBytes);
124
125    void addProgramToDirty(const Program *);
126    void removeProgramToDirty(const Program *);
127
128    virtual void dumpLOGV(const char *prefix) const;
129    virtual void serialize(Context *rsc, OStream *stream) const;
130    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
131    static Allocation *createFromStream(Context *rsc, IStream *stream);
132
133    bool getIsScript() const {
134        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
135    }
136    bool getIsTexture() const {
137        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
138    }
139    bool getIsRenderTarget() const {
140        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
141    }
142    bool getIsBufferObject() const {
143        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
144    }
145
146    void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
147    void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
148    virtual bool freeChildren();
149
150    void sendDirty(const Context *rsc) const;
151    bool getHasGraphicsMipmaps() const {
152        return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
153    }
154
155    void * getSurface(const Context *rsc);
156    void setSurface(const Context *rsc, RsNativeWindow sur);
157    void ioSend(const Context *rsc);
158    void ioReceive(const Context *rsc);
159
160protected:
161    Vector<const Program *> mToDirtyList;
162    ObjectBaseRef<const Type> mType;
163    void setType(const Type *t) {
164        mType.set(t);
165        mHal.state.type = t;
166    }
167
168#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
169    class NewBufferListener : public android::ConsumerBase::FrameAvailableListener {
170    public:
171        const android::renderscript::Context *rsc;
172        const android::renderscript::Allocation *alloc;
173
174        virtual void onFrameAvailable();
175    };
176
177    sp<NewBufferListener> mBufferListener;
178    sp< GrallocConsumer > mGrallocConsumer;
179#endif
180
181
182private:
183    void freeChildrenUnlocked();
184    Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
185
186    uint32_t getPackedSize() const;
187    static void writePackedData(Context *rsc, const Type *type, uint8_t *dst,
188                                const uint8_t *src, bool dstPadded);
189    void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
190    void packVec3Allocation(Context *rsc, OStream *stream) const;
191};
192
193}
194}
195#endif
196
197